0
def process_player_choice():
    print('what is your choice? enter 1 for rock, 2 for paper, or 3 for  scissors: ')
    choice2 = int(input())
    while choice2 != 1 and choice2 !=2 and choice2 != 3:
        print('error: the choice can only be 1,2 or 3.')
        choice2 = int(input('please enter a correct choice: '))
    return choice2

i need my program to print the error message when they type something other than 1,2 or 3. for example if i type "c" or "1.5" my program crashes. the error message i get is "builtins.ValueError: invalid literal for int() with base 10: '1.5'". if you want to look at the whole program heres the link https://gist.github.com/anonymous/ee43c2a593bae22a5dec99c9cb1dd26c

  • Because you are using `int` to convert to an integer. What if the input isn't a valid integer? Then it will throw the `ValueError`. – Andrew Li Oct 30 '16 at 22:45
  • what can i do to fix it? im not to familiar with coding. – user7092992 Oct 30 '16 at 22:47
  • My advice: learn some python before diving right in. How you can 'fix' this is to use a try/except to catch the error, and reprompt the user if needed – Andrew Li Oct 30 '16 at 22:48
  • 2
    Leave the input as a string and don't convert it to an integer, since you do no calculation with it. You can use `choice2 = input()` and `while choice2 != '1' and choice2 !='2' and choice2 != '3':` or better `while choice2 not in ('1', '2', '3'):` – Rory Daulton Oct 30 '16 at 22:56
  • @AndrewLi Or the alternative is to test if the user input is an integer before trying to convert it to one. But the first way you mentioned is consider more "Pythonic", and is more robust. – Christian Dean Oct 30 '16 at 22:57

0 Answers0