while loop with the if statement in python -


print ("selection of mode\n easy \n moderate \n tough") selection = int(input("please enter right choice!! > "))  while selection != '':     if selection == 1:         print("\ngoodluck")     elif selection == 2:         print('\nyou brave')     elif selection == 3:         print ('\nyou must kidding')     selection = int(input("please enter right choice!!! > ")) 

i making small program, new python have bit of understanding problem, need know error when user have typed in correct selection 1 or 2 or 3 should go in loop , run if statement. once done should exit. apparently not working perfectly.

print ("selection of mode\n easy \n moderate \n tough") selection = input("please enter right choice!! > ")  while selection != '':     if selection == '1':         print("\ngoodluck")         break     elif selection == '2':         print('\nyou brave')         break     elif selection == '3':         print ('\nyou must kidding')         break     selection = input("please enter right choice!!! > ") 

i doing goes through if statement want exit once first selection correct of if statement.

try this

print ("selection of mode\n easy \n moderate \n tough") selection = int(input("please enter right choice!! > "))  while selection != '':     if selection == 1:         print("\ngoodluck")         break     elif selection == 2:         print('\nyou brave')         break     elif selection == 3:         print ('\nyou must kidding')         break     selection = input("please enter right choice!!! > ") 

also there no need of input() multiple times.

print ("selection of mode\n easy \n moderate \n tough") while true:     selection = int(input("please enter right choice!!! > "))     if selection == 1:         print("\ngoodluck")         break     elif selection == 2:         print('\nyou brave')         break     elif selection == 3:         print ('\nyou must kidding')         break 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -