3

This code:

import random
print("\tWelcome to the guess my number program\n")

print("im thinking of a number between 1 and 100")
print("try to guess it in 5 attempts\n")

randomNum = random.randint(1,101)

tries = 0

userNumber = int(input("please pick a number"))

while userNumber != randomNum:
    if userNumber > randomNum:
        print ("lower") 
    else:
        print("higher") 
    tries += 1

print ("you guessed it! the number was" , randomNum)

For some reason this produces an infinite loop. Any help, im still getting used to python.

4
  • 3
    I don't think this question deserves a downvote. The title is explanatory enough and although the OP is making an elementary mistake, we all did the same when we started. Commented Jul 2, 2013 at 18:00
  • i'm finding it hard to tell whether the indentation used within the while loop is on purpose or copy+paste formatting error or just a programmatic mistake. Commented Jul 2, 2013 at 18:02
  • @Relfor The code wouldn't run if OP had actually written his code that way. Can't have an infinite loop if you have an IndentationError. :) Commented Jul 2, 2013 at 18:04
  • Besides the answer you got, also keep in mind that you prompt the user to try and guess in 5 attempts, and keep a counter of the number of tries, but you apparently forgot to do anything with it - like preventing more than 5 tries or at least printing back how many guesses it took the user. Commented Jul 2, 2013 at 18:05

3 Answers 3

5

You never updated your userNumber or randomNum inside the while loop. So once the loop condition is met, it will execute infinitely.

You need to update your while loop as:

while userNumber != randomNum:
    if userNumber > randomNum:
        print ("lower") 
    else:
        print("higher") 

    tries += 1    
    userNumber = int(input("please pick a number"))
Sign up to request clarification or add additional context in comments.

2 Comments

so the tries and userNumber are still in the loop itself? Also i have to ask for the userNumber twice in order for this too work? I actually wrote a similar program in java but i dont remember how i did it.
Yes, they both should be inside the loop only. And if you just want the give user two chance, then add the tries in the while loop condition.
3

You've forgotten to ask the user to guess again. Try this!

while userNumber != randomNum:
    if userNumber > randomNum:
        print("lower")
    else:
        print("higher")
    tries += 1
    userNumber = int(input("please pick a number")

Comments

0

Try This:

import random
print("Welcome to the guess my number program!")
randomnum = random.randint(1, 100)
print("I'm thinking of a number between 1 and 100.")
print("Try to guess it in 5 attempts.")
tries = 0
usernumber = 0
while not usernumber == randomnum:
   usernumber = int(raw_input("please pick a number.")
   if usernumber > randomnum:
      print("Too High")
   if usernumber < randomnum:
      print("Too Low")
   tries = tries + 1
if usernumber == randomnum:
   print("You got the number!")
print("It took you " + str(tries) + " attempts.")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.