I am learning Python and it is my first programming language. So I tried making a number guessing game where the goal is to guess a number that is within 1 and 100. Every time the player inputs a guess, the program will update the range of possibility. Example: the answer is 50, the player guess 40, then the program would prompt the player to guess a number between 40 and 100... until the player gets the number. Here is my working code:
ans=input('Please enter a number between 1 and 100: ')
guess=input('Please enter your guess: ')
low=1
upp=100
while guess!=ans:
if ans>guess:
low=int(guess)
elif ans<guess:
upp=int(guess)
guess=input('The answer is between %d and ' %low + '%d' %upp)
else:
print('You have guessed correctly!')
This code works as intended, however within the While loop for the assignments of variable 'low' and 'upp', the program would not work if I put 'upp=guess' instead. A TypeError would occur, and it seems like 'upp' (or 'low') has become a string instead of a number.
My question is how did the variable 'upp' become a string while the variable 'guess' is a number?