Python Forum
Variable defined but python wont recognize it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable defined but python wont recognize it.
#1
Hi, i'm making a program that is supposed to randomly generate numbers between the parameters given.

I just copied a bit of the code to show the problem:
while True:
    command = input("\nEnter command: ")

    # Depending of the command that has been entered do the
    # thing related to the command.
    if command.lower() == 'help':
        show_help()
    if command.lower() == 'integer':
        while True:
            type_of_nums = input(type_of_nums_prompt)
            if type_of_nums == 'back':
                break
            if type_of_nums.lower() == 'e':
                repeated_q = input("Can the numbers be repeated? (y/n) ")
                if repeated_q.lower() == 'y':
                    try:
                        nums_to_gen = input(nums_to_gen_prompt)
                        nums_to_gen = int(nums_to_gen)
                        min_num = input(min_num_prompt)
                        min_num = int(min_num)
                        max_num = input(max_num_prompt)
                        max_num = int(max_num)
                        if max_num == 0:
                            print("Thats not a valid number!\n")
                            break
                    except ValueError:
                        if nums_to_gen == 'back' or min_num == 'back' or max_num == 'back':
                            break
                        else:
                            print("That is not a number!\n")
                            continue
                    else:
                        e_repeated(nums_to_gen, min_num, max_num)
                        break
And this is the error:
Error:
Welcome to Random Number Generator v1.0 Enter <help> for a list of commands and <quit> to exit the program. Enter command: integer What kind of number should the program generate? e Can the numbers be repeated? (y/n) y How many numbers do you want to generate? qfef Traceback (most recent call last): File "main.py", line 39, in <module> nums_to_gen = int(nums_to_gen) ValueError: invalid literal for int() with base 10: 'qfef' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "main.py", line 48, in <module> if nums_to_gen == 'back' or min_num == 'back' or max_num == 'back': NameError: name 'min_num' is not defined
As you can see in the first code the min_num variable is defined but it wont work when i use it in the if test.
Also if you want to check the whole code you can go to: https://github.com/fwendeburg/random-num...evelopment
Thanks in advance!
Reply
#2
because of the try/except clause, if you enter invalid literal that cannot be converted to int it will raise ValueError and will jump to except block. There you try to use nums_to_gen, min_num, max_num. Depending on which line (18, 20 or 22) is the invalid input some or all of these will not be defined. Thus the second error. In your case you enter qfef for num_to_gen, so min_num and max_num are not defined when you execute the except block (i.e. you have never executed lines 19-22)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Quote:
Traceback (most recent call last):
  File "main.py", line 39, in <module>
    nums_to_gen = int(nums_to_gen)
ValueError: invalid literal for int() with base 10: 'qfef'

If the try block fails before the definition of min_num then of course its not going to be defined. You need to restructure your program or use isdigit.
Recommended Tutorials:
Reply
#4
crosspost at https://www.reddit.com/r/learnpython/com...thon_wont/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 126 Yesterday, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 517 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Why wont this path work one way, but will the other way? cubangt 2 623 Sep-01-2023, 04:14 PM
Last Post: cubangt
  python cant recognize PIP siubikYT 2 710 Jul-19-2023, 06:30 PM
Last Post: Lahearle
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,045 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,129 Feb-25-2022, 06:31 PM
Last Post: jao
  My program won't recognize the filename. braingoblins 1 1,096 Jan-07-2022, 06:18 PM
Last Post: deanhystad
Star NameError – function doesn't recognize imported modules Sir 4 3,419 Dec-01-2020, 06:36 AM
Last Post: Sir
  Function will not return variable that I think is defined Oldman45 6 3,436 Aug-18-2020, 08:50 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020