Basic error in python

Asked

Viewed 83 times

0

I created this script, but is giving this following error:

print('Choices\n1-Option 1\n2-Option 2\n3-Option 3')


if choose =='1':
    print('The account is 5+5')
elif choose =='2':
    print('The account is 6+6')
elif choose =='3':
    print('The account is 7+9')


else:
    print('You choose none options')    

Error:

Traceback (most recent call last):
File "main.py", line 11, in <module>
 if choice =='1':
 NameError: name 'choice' is not defined

Can anyone tell me what is wrong? Since a few days ago the program ran normally? By the way, I’ve driven in python, pycharm and go online, and nothing working

  • Can I edit your question? There is part unformatted Python code.

  • Where’s the full code? You declared Choice earlier?

  • 2

    @Maurydeveloper the tag [python] in the question already applies Highlight in the post, no need to add explicitly in this case with ````Pyton, just the tag in the question that the system does the rest.

  • Okay. Thanks for the info.

1 answer

3

The Choice variable has not been defined, does not exist, does not have because the code works, the code has no way to guess what you wrote inside a string in a print:

print('Choices\n1-Option 1\n2-Option 2\n3-Option 3')

What must be missing is the input to get what the user enters the/cmd/etc terminal, you probably wanted something like:

print('Choices\n1-Option 1\n2-Option 2\n3-Option 3')

choose = input()

if choose =='1':
    print('The account is 5+5')
elif choose =='2':
    print('The account is 6+6')
elif choose =='3':
    print('The account is 7+9')
else:
    print('You choose none options')
  • More information on declaring a Python variable: https://www.guru99.com/variables-in-python.html

  • @Maurydeveloper a whole tutorial to teach how to use the =? I think it’s a bit of an exaggeration, but okay, maybe it’s worth the hint. Still a good source would be the official documentation. On variables I would go a little further, because the tutorial ai is a little weak (despite saying something important like the global), some links that might interest you: https://answall.com/a/307826/3635 and https://answall.com/a/240667/3635 (although this second is more about arguments, it still speaks about the imutaveis and the non)

  • I learned Python on this page,so I sent it,I just wanted help. And thanks for the links I will see it and study.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.