-1

I'm trying to make a small game using Python3. There are 3 options in the said game giving the user the option to input '1' '2' or '3', right now if anything other than an integer is inputted it crashes with the message:

Traceback (most recent call last):
  File "main.py", line 62, in <module>
    user_choice = int(input("""
ValueError: invalid literal for int() with base 10: 'fj

For reference heres the code i've been trying:

 while i < round_select: #Loops for value of round_select
  i += 1 #adds value to round_select every round until user input met

  opponent = random.randint(1,3)   #generates random integer printed as paper, scissors, or rock by above function

  

    time.sleep (0.2)
      user_choice = int(input("""
      (1)Paper, (2)Scissors, (3)Rock!: """))

      if user_choice == opponent:
        print (username + choice_to_text(user_choice)) 
        print ("Opponent" + choice_to_text(opponent)) 
        print ("Tie")
        ties += 1 
      elif (user_choice == 1 and opponent == 2)  or (user_choice == 2 and opponent == 3) or (user_choice == 3 and opponent == 1): #outcomes grouped together to avoid mountains of elif statements
        print (username +  choice_to_text(user_choice))
        print ("Opponent"  + choice_to_text(opponent))
        print ("One point to the opponent!")
        opponent_score += 1
      elif (user_choice == 1 and opponent == 3) or (user_choice == 2 and opponent == 1) or (user_choice == 3 and opponent == 2):
        print (username + choice_to_text(user_choice))
        print ("Opponent" + choice_to_text(opponent))
        print ("One point to " + (username) + "!")
        user_score += 1
      elif user_choice != int or user_choice == ValueError:
        print ("Please type an integer between 1 and 3 ")
        i -= 1

edit: Looked like some people were trying to point me towards another question (How to check if string input is a number). I went to this before posting the question and couldn't find anything helping out so i went to write this. Thank you though to everyone who tried to help , It is working now :)

  • 1
    Does this answer your question? [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – Gino Mempin May 05 '21 at 03:51
  • Also related: [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/2745495) – Gino Mempin May 05 '21 at 03:53

2 Answers2

2

Removing the int() from your input statement should work. It might be easier to make the random number a string so you don't have issues with the different data types.

user_choice = input("""(1)Paper, (2)Scissors, (3)Rock!: """)

opponent = str(random.randint(1,3))

This happens because python is trying to transform a character to an integer, you already account for that case in the last elif. Any other answer that is not an integer from 1 to 3 will raise the Error print statement

while i < round_select: #Loops for value of round_select
    i += 1 #adds value to round_select every round until user input met
    
    opponent = str(random.randint(1,3))   #generates random integer printed as paper, scissors, or rock by above function
    
    time.sleep (0.2)
    user_choice = input("""(1)Paper, (2)Scissors, (3)Rock!: """)

    if user_choice == opponent:
        print (username + choice_to_text(user_choice)) 
        print ("Opponent" + choice_to_text(opponent)) 
        print ("Tie")
        ties += 1 
    elif (user_choice == '1' and opponent == '2')  or (user_choice == '2' and opponent == '3') or (user_choice == '3' and opponent == '1'): #outcomes grouped together to avoid mountains of elif statements
        print (username +  choice_to_text(user_choice))
        print ("Opponent"  + choice_to_text(opponent))
        print ("One point to the opponent!")
        opponent_score += 1
    elif (user_choice == '1' and opponent == '3') or (user_choice == '2' and opponent == '1') or (user_choice == '3' and opponent == '2'):
        print (username + choice_to_text(user_choice))
        print ("Opponent" + choice_to_text(opponent))
        print ("One point to " + (username) + "!")
        user_score += 1
    elif user_choice != int or user_choice == ValueError:
        print ("Please type an integer between 1 and 3 ")
        i -= 1
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
0

The Pythonic way to deal with this is called EAFP: Easier to ask for forgiveness than permission. Wrap the code with try and except statements so you can detect when something invalid is entered.

try:
  user_choice = int(input("""
  (1)Paper, (2)Scissors, (3)Rock!: """))
except ValueError:
  # do something here like display an error message and set a flag to loop for input again

If that doesn't suit your style, it's easy to check a string to ensure it can be converted to an integer before you do it:

bad_input = True
while bad_input:
  user_choice = input("""
  (1)Paper, (2)Scissors, (3)Rock!: """)
  if user_choice.isnumeric():
    user_choice = int(user_choice)
    if user_choice in (1, 2, 3):
      bad_input = False
    else:
      print("Bad input, try again.")
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622