-4

Compare two variables named target and guess when one is an integer, and one is a string using Python 3.

import random
import sys
target = random.randint(1, 20)
name = input ('Hello, what is your name?')
print ('Hello, %s. I am thinking of a number from 1 to 20. You will have 3 tries, and after each try, I will tell you if the number that I am thinking of is lower or higher. Try to guess it!' % name) 
guess = input ('What number do you think I am thinking of?')
if guess == target:
    print ('Congratulations! You won! Please play again!')
    sys.exit()
else:
    print ('You did not guess the number correctly.')
    if target < guess:
        print ('The number that I am thinking of is smaller than your guess. Try again')
    else:
        print ('The number that I am thinking of is larger than your guess. Try again!')
Munim Munna
  • 17,178
  • 6
  • 29
  • 58

1 Answers1

0

You can simply parse the input from string to an integer in this manner:

guess = int(input('What number do you think I am thinking of?'))

And then you can freely compare it to any integer you'd like.

Nick S.
  • 16
  • 1
  • 6