Friday 15 April 2011

python - Script crashes if type wrong or nothing -


This is a simple menu. If I type a wrong number or none of the script crashes.

  Print "1) Menu 1" Print "2) Menu '2' Print" Q) Exclude Choice 'raw_input ("?" Gt ""') Option 'q' or 'Q' Is:   
  if option == '1': print "menu1"   

there is a single loop here

  elif choice == '2': print "menu2" else: print "invalid option" # want to resume the input question from here "print off the loop"   

while option is not 'q' or 'q': is parsed

  while (option is not 'q') Or 'Q':   

Regardless of the value of such a preference, Q is correct as a non-empty string As you never change the value of the choice in the loop, it will never end there option '[q', 'q']: print "1" menu 1 "print" 2 ) If you are not in the menu 2 "print" q, you want to do something else

  like = '') "likes" = raw_input ("? "You have selected 2": "You chose 2"   

Why not want to use :

 >> Foo = 'Q'.lower ()>> Foo & # 39; q> & gt; Foo' q '>>> Foo ==' q 'true>>> Foo is' q' wrong   

options For other suggested changes to determine if there is an upper or lowercase 'q': Anyone who finds you the most readable. Your program is waiting to spend a lot of time that you You will spend the key to determining this key as compared to being suppressed, so do not worry about which one is faster than the other.

No comments:

Post a Comment