0

I'm trying to code a little game here to show someone, but I'm having trouble with it. When I try a sum of two values, it just says TypeError: must be str, not int. I am newbie, so I wasn't able to find a fix. I programmed it in another language (not another programming language, I mean I programmed it in portuguese but translated it to english so it is easier to understand). Here you have the code:

import random
import time

print ('Welcome to the Even or Odd game!')
print ('Type the letter \'o\' if you want odd and \'e\' if you want even.')

userChoice = input('Your choice: ').lower()
time.sleep(1)
userNumber = input('Now, type in a number from 1 to 5: ')

randomNumber = random.randint(1,5)
time.sleep(2)

print ('Your number: ' + str(int(userNumber)))
time.sleep(2)
print ('Computer\'s number: ' + str(int(randomNumber)))
time.sleep(2)

result = userNumber + randomNumber
print ('Adding these two numbers together, you have ' + str(result))
if result % 2 == 0:
    if userChoice == 'e' or 'even':
        print ('You won!')
    else:
        print ('You lost!')
else:
    if userChoice == 'o' or 'odd':
        print ('You won!')
    else:
        print ('You lost!')

I get the TypeError if I run this program. It happens when it tries to execute result = userNumber + computerNumber. Any ideas? Thank you so much!

vaultah
  • 44,105
  • 12
  • 114
  • 143
teolicht
  • 245
  • 2
  • 6
  • 13

1 Answers1

1

When you take the user's input, it is being stored as a string. You're getting the error because you're trying to add a string and an integer.

You can use the int() function to turn a string to an integer. So you can do for example:

result = int(userNumber) + int(randomNumber)