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.
Reply
#4
crosspost at https://www.reddit.com/r/learnpython/com...thon_wont/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star NameError – function doesn't recognize imported modules Sir 4 1,135 Dec-01-2020, 06:36 AM
Last Post: Sir
  Function will not return variable that I think is defined Oldman45 6 1,385 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 1,244 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Variable not defined Heyjoe 4 990 Jul-10-2020, 11:27 PM
Last Post: Heyjoe
  python library not defined in user defined function johnEmScott 2 993 May-30-2020, 04:14 AM
Last Post: DT2000
  Error: variable can not be defined julio2000 2 1,527 Feb-09-2020, 08:51 PM
Last Post: julio2000
  Player object wont recognize collision with other objects. Jan_97 3 1,145 Dec-22-2019, 04:08 PM
Last Post: joe_momma
  How to recognize space or enter as one-character input? Mark17 5 1,623 Oct-17-2019, 08:19 PM
Last Post: jefsummers
  Pynput doesn't recognize shift button and special characters VirtualDreamer 0 1,557 Jul-17-2019, 11:55 AM
Last Post: VirtualDreamer
  Why wont this work? ejected 2 1,738 Mar-29-2019, 05:33 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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