0

I'm trying to make a simple number guessing game. I wanted to try to see if I could do this myself without looking up any answers but I'm very confused on how to keep the game going if the guess is not correct. Here is what I have so far:

import random

#ask user to guess a number:
guess = int(input("Guess a number from 0 to 100: \n"))

#create random number:
computer_number = random.randint(0, 100)

#How can i make this block of code loop to keep on giving the user tries??

if guess == computer_number:
    print("You won")
elif guess > computer_number:
    print("Try a lower number!")
else:
    print("Try a higher number!")
  • 1
    Try a `while` loop. – Dominik Stańczak Jul 28 '22 at 05:08
  • I tried inputting "while guess != computer_guess:" in front of my "if" statement but it went into an infinite loop printing my else or elif statements – jeffcojake Jul 28 '22 at 05:10
  • Welcome to Stack Overflow. "but it went into an infinite loop printing my else or elif statements " Try to think about the problem logically. Should the user be asked to make a guess (always, regardless) one time, or (potentially) multiple times? Therefore, should the code to ask for a guess go inside the loop, or outside? Think about the condition: `while guess != computer_guess:`. Suppose the condition is satisfied right now. If neither `guess` nor `computer_guess` changes, can the condition ever stop being satisfied? Therefore, would the loop end? Which one should change? – Karl Knechtel Jul 28 '22 at 05:28

3 Answers3

1
import random

#ask user to guess a number:


#create random number:
computer_number = random.randint(0, 100)


#How can i make this block of code loop to keep on giving the user tries??
while True:
    guess = int(input("Guess a number from 0 to 100: \n"))
    if guess == computer_number:
        print("You won")
        break
    elif guess > computer_number:
        print("Try a lower number!")
    else:
        print("Try a higher number!")
doc
  • 828
  • 2
  • 6
  • 18
  • Thanks for the fix. Works great except it asks for an input twice before printing out if the number is higher or lower – jeffcojake Jul 28 '22 at 05:16
  • No it doesn't. Look closely. Notice that this code has *removed* the call to `input` from before the loop, because it does not make sense. Make sure you understand how the logic works, step by step. (Also check out the duplicate I linked.) – Karl Knechtel Jul 28 '22 at 05:29
0

You could use a while loop to loop the game until a condition is met.

For example:

#create random number:
computer_number = random.randint(0, 100)

while True:
  #ask user to guess a number:
  guess = int(input("Guess a number from 0 to 100: \n"))

  #How can i make this block of code loop to keep on giving the user tries??

  if guess == computer_number:
      print("You won")
      break
  elif guess > computer_number:
      print("Try a lower number!")
  else:
      print("Try a higher number!")
Lukas
  • 110
  • 11
0

it's very easy you just need an while loop:

while condition:
    #while condition True run what stands here

So the answer to your question is:

import random

#create random number:
computer_number = random.randint(0, 100)

guess = -1 
#we need to firs declare the variables so we can use them in the condition

while guess != computer_number:
    #ask user to guess a number:
    guess = int(input("Guess a number from 0 to 100: \n"))
  
    if guess == computer_number:
        print("You won!")
    elif guess > computer_number:
        print("Try a lower number!")
    else:
        print("Try a higher number!")