0

Im trying to make it so the warmer/colder text appears after input, but it doesn't print until the end of the program?

I think it has something to do with the elif statement? i keep having trouble with those in other projects (had to put hangman on the backburner because of it.) I'm trying to make a program where you guess the a number between 1-50 but i just don't know how to rectify this problem?

import random

print("Welcome to the number guessing game!")
print("")


guesses_allowed = 10
number = random.randint(1, 50)
guess = ""
for i in range(guesses_allowed): 
    guess_number = int(input("Please guess a number between 1 and 50: "))
    guess = int(guess_number)

if guess == number:
   print("Congrats you guessed correctly") 

elif guess < number:
   print("number is higher.")
else:
   print("number is lower.")

if (i == guesses_allowed - 1):
    print("Sorry, have run out of guesses")

if abs(guess - number) <= 10:
    print("You're warm!")
elif abs(guess - number) <= 20:
        print("You're getting warmer.")
elif abs(guess - number) <= 30:
        print("You're cold")

else: 
    print("You're freezing")
  • 2
    what have you tried so far ? the question needs sufficient code for a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – D.L Aug 15 '23 at 11:55
  • 2
    The code starting with `if guess == number` should all be indented one level so that it appears within the body of the for-loop. It doesn't make sense to get 50 inputs in a row and only then analyze the last input. If you do put the code inside of the for-loop, note that you would need to break out of the loop in the event that a guess is correct. – John Coleman Aug 15 '23 at 12:34

1 Answers1

0

If you indent line 14 till the end one tab, that code will also be in the body of the for-loop. This means that the constraints are checked every round instead of only at the end, and the game works correctly.

Jelle
  • 198
  • 9