Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve NameError: name 'xx' is not defined?

I am learning python, according to the logic written in the code in the book, I want to see the running result, the code is as follows, but the output error NameError: name 'pr' is not defined

code show as below:

stack=[]
def pushit():
    stack:append(input(' Enter New String: ').strip())
def popit():
    if len(stack)==0:
        print('Cannot pop from an empty stack!')
    else:
        print ('Removes [','stack.pop()',']')
def viewstack():
    print(stack)
CMDs={'u':pushit,'o':popit,'v':viewstack}
def showmenu():
    pr=''' 
p(U)sh
p(O)p
(V)iew
(Q)uit
Enter choice:'''
while True:
    while True:
        try:
            choice=input(pr).strip()[0].lower()
        except (EOFError,KeyboardInterrupt,IndexError):
            choice='q'
        print('\nYou picked:[%s]'%choice)
        if choice not in 'uovq':
            print('Invalid option,try again')
        else:
            break
        if choice=='q':
            break
        CMDs[choice]()
if _name_=='_main_':
    showmenu()

The error message is as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Traceback (most recent call last):
  File "/Users/oliver/Desktop/test.py", line 22, in <module>
    choice=input(pr).strip()[0].lower()
NameError: name 'pr' is not defined

>Solution :

There are two errors in your code,

  1. The logic in the showmenu method needs to be indented
  2. _name_=='_main_' -> __name__ == "__main__"
stack=[]
def pushit():
    stack:append(input(' Enter New String: ').strip())
def popit():
    if len(stack)==0:
        print('Cannot pop from an empty stack!')
    else:
        print ('Removes [','stack.pop()',']')
def viewstack():
    print(stack)
CMDs={'u':pushit,'o':popit,'v':viewstack}
def showmenu():
    pr=''' 
    p(U)sh
    p(O)p
    (V)iew
    (Q)uit
    Enter choice:'''
    while True:
        while True:
            try:
                choice=input(pr).strip()[0].lower()
            except (EOFError,KeyboardInterrupt,IndexError):
                choice='q'
            print('\nYou picked:[%s]'%choice)
            if choice not in 'uovq':
                print('Invalid option,try again')
            else:
                break
            if choice=='q':
                break
            CMDs[choice]()

if __name__ == "__main__":
    showmenu()
 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading