Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

python - Green horn can't understand why input only triggers else, ignores if & elif even if input is true

I'm trying to "convert" an old arithmetic tester I wrote, for my children in "Commodore" BASIC about 100 years ago and which worked fine. I'm having great problems trying to get the "arithmetic selection" stage working, as it fails on the if/elif when choice is input, simply defaults to else.

I can get it working if I convert the class of input to int() and the selection menu to numeric but I can't get it working simply using the str() class & alphabetic menu. It is probably a lot easier for a guru to see the two lots of code and see the glaringly obvious error I made, so I'll try to do that.

int() version:

a = int()
s = int()
m = int()
d = int()
print( '''         This is an input tester script.

 To start the program, please select one of the functions below, by typing the number of that function at the prompt.''')

choice = int(input(''' Please type in your choice of arithmetic. 

    1 for addition 
    2 for subtraction 
    3 for multiplication 
    4 for division 

   '''))

if choice == 1 : print(' 111')

elif choice == 2 : print('  222')

elif choice == 3 : print('  333')

elif choice == 4 : print(' 444')
else : print('   Wrong keys')

str() version:

a = str()
s = str()
m = str()
d = str()
choice = str()

choice = input('''Please select your sums, using:

  a for addition  
  s for subtraction 
  m for multiplication 
  d for division 
 
 ''')

if choice == a : print ('well done')
elif choice == s : print('that's 2')#execfile (subtract)
elif choice == m : print('that's 3')#execfile (subtract)
elif choice == d : print('that's 4')#execfile (divide)
else: print('Invalid selection')
question from:https://stackoverflow.com/questions/65832682/green-horn-cant-understand-why-input-only-triggers-else-ignores-if-elif-even

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your second code is not working because you have not quoted a,s,m,d in condition making because of that it is acting like a variable.see here↓

if choice == a : print ('well done')

Here a is acting like a variable and you have put the value of variable a as empty string sting so it doesn't match with the input value and thus return 'Invalid selection' as output.

It should be ↓

if choice == 'a' : print ('well done')

See here how you should write that code.

# a = str()   #There is no use to define these as a empty string
# s = str()
# m = str()
# d = str()
# choice = str()       

choice = input('''Please select your sums, using:

  a for addition  
  s for subtraction 
  m for multiplication 
  d for division 
 
 ''')

if choice == 'a' : print ('well done')
elif choice == 's' : print('that's 2')#execfile (subtract)
elif choice == 'm' : print('that's 3')#execfile (subtract)
elif choice == 'd' : print('that's 4')#execfile (divide)
else: print('Invalid selection')

OR if you want to keep a,s,m,d as a variable than↓

a = 'a'
s = 's'
m = 'm'
d = 'd'
#choice = str()

choice = input('''Please select your sums, using:

  a for addition  
  s for subtraction 
  m for multiplication 
  d for division 
 
 ''')

if choice == a : print ('well done')
elif choice == s : print('that's 2')#execfile (subtract)
elif choice == m : print('that's 3')#execfile (subtract)
elif choice == d : print('that's 4')#execfile (divide)
else: print('Invalid selection')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...