UnboundLocalError: TRY EXCEPT STATEMENTS

2024/11/16 9:33:44

I am currently creating a menu with try except tools. I'm trying to create it so if a user enters nothing (presses ENTER) to output:

You have not entered anything, please enter a number between 1 and 4

This is the error message(click here)

This is what I've done so far:

def Menu():print("""Hello, please enter a number 1 - 41 - Compliment2 - Fact3 - Insult4 - Quit""")try:UserInput_INT = int(input("> "))except ValueError:UserInput_STR = (UserInput_INT)if len(UserInput_STR) == 0:print("You have entered nothing. Please enter a number between 1 and 4")print("You entered a character. Please enter a number between 1 and 4")Menu()if UserInput_INT not in range (1, 5):print("Invalid input please enter a whole number between 1 and 4")UserInput_STR = (str(UserInput_INT))if UserInput_STR == '1':print(" You look horrible today!")elif UserInput_STR == '2':print("Sam Birkenshaw & Jordan Ives can code better than Mr Bath. ")elif UserInput_STR == '3':print("You are bad at coding ")elif UserInput_STR == '4':quit()Menu()
Answer

In the statement UserInput_STR = (UserInput_INT) [*] you are accessing a variable that you did not initalize, since int(input("> ")) have failed. Here's a shorter example:

def f():try:x = 2/0except:print(x)

What should print(x) do? the variable is indeed local, but is not bound. It is not initialized.

[*] I believe you meant something like UserInput_STR = str(UserInput_INT), but it won't work either for the same reason.

https://en.xdnf.cn/q/119543.html

Related Q&A

Cant load music into pygame

please help if you can. Cant seem to be able to upload music into my game in progress. It comes up with the error of "cant load"... Would be great if someone got back to me quick, This is a m…

C# Socket: how to keep it open?

I am creating a simple server (C#) and client (python) that communicate using sockets. The server create a var listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp)then …

Python Selenium - how to get confirmation after submit

I have a follow up question on this post, I want to get any confirmation text after I hit submit button. Either the code works or not. html - invalid example <div class="serialModalArea js-seri…

Assigning a input on a line to its line number?

I was having a go at the following problem from the AIO (Australian Informatics Olympiad) Training Problems Site (Question in Italics and specifics in bold, my attempt below): The Problem Encyclopaedia…

Create a new list according to item value

I have a list like below. [T46, T43, R45, R44, B46, B43, L45, L44, C46, C45]where I want to group according to int value:[id][ , , , , ] # AREA PATTERN [Top, Right, Bottom, Left, Center][46][1,0,1…

Concatenating many time and date columns

I have many date and time column pairs (around 15 each) that share the same prefix, ie. SH or DEL. The columns are all of dtype object, ie. string. All the columns belong to the same Dataframe. Here is…

Python with ICS files and CSV [duplicate]

This question already has answers here:Parsing files (ics/ icalendar) using Python(6 answers)Closed 5 years ago.one friend ask me some help on a personnal project, but I have to admit my skills in Pyth…

loading my data in numpy genfromtxt get errors

I have my data file contain 7500 lines with :Y1C 1.53 -0.06 0.58 0.52 0.42 0.16 0.79 -0.6 -0.3 -0.78 -0.14 0.38 0.34 0.23 0.26 -1.8 -0.1 -0.17 0.3…

Install dlib with cuda support ubuntu 18.04

I have CUDA 9.0 and CUDNN 7.1 installed on Ubuntu 18.04(Linux mint 19). Tensorflow-gpu works fine on GPU(GTX 1080ti).Now i am trying to build dlib with CUDA support:sudo python3 setup.py install --yes …

View 3 dimensional Numpy array in Matplotlib and taking arguments from Keyboard or mouse

I have 3 dimensional data say (5,100,100). Now I would like to see them slice by slice upon hitting the down arrow button.