Finlay Ekins
Finlay Ekins

Reputation: 9

Python guess the number game

I tried to make a guess the number game in python but whenever I guess it repeats 4 times 'your guess is too low'

import random
number = random.randint(1, 20)
guessestaken = 0
print('I am thinking of a number between 1 and 20 ')
guess = raw_input('Take a guess and hit enter')
while guessestaken < 4:
    guessestaken = guessestaken + 1
    if guess > number:
        print('your number is too low')
    if guess < number:
        print('your number is too high ')
    if guess == number:
        break
        print('well done the number was ' + number + ' and you got it in ' + guessestaken + '')

Upvotes: 0

Views: 1100

Answers (5)

BatteTarte
BatteTarte

Reputation: 59

You just tried to make a loop in >4 so it is a normal error just to use while True: and change the raw_input to int(raw_input)

import random
number = random.randint(1, 20)
guessestaken = 0
print('I am thinking of a number between 1 and 20 ')
guess = int(raw_input('Take a guess and hit enter'))#make your input a int
while True:#change <4 for True
    guessestaken = guessestaken + 1
    if guess > number:
        print('your number is too low')
    if guess < number:
        print('your number is too high ')
    if guess == number:
        break
        print('well done the number was ' + number + ' and you got it in ' + guessestaken + '')

Upvotes: 0

mujad
mujad

Reputation: 703

from random import randint

print("you wanna guess a number between A to B and time of guess:")
A = int(input("A:"))
B = int(input("B:"))
time = int(input("time:"))
x = randint(1, 10)
print(x)

while time != 0:
    num = int(input("Enter: "))
    time -= 1
    if num == x:
        print("BLA BLA BLA")
        break
    print("NOPE !")
    if time == 0:
        print("game over")
        break

Upvotes: 0

user3578017
user3578017

Reputation:

You are asking for the user input before the while loop.

guess = int(raw_input('Take a guess and hit enter')) 

This statement should come within the while block.

The function raw_input returns a string, you should convert it to an integer. You can read more about it in the Documentation.

Upvotes: 1

JHobern
JHobern

Reputation: 864

You are asking for the input from the user only once, right before you enter your loop. You will need to ask the user for a new input after every iteration of the loop, otherwise the guess will never change!

Additionally, when you read in some input from the user with raw_input it will be a string. You will need to cast it to an int. Next, if you have a break in your loop, the statements after it will not get called. This means that you need to move the break statement to after the output when the user gets the right answer, or nothing will be printed. Lastly, your logic in the if statements is backwards, if the guess is less than your generated number then the guess was too low, not too high! Altogether you get:

import random
number = random.randint(1, 20)
guessestaken = 0
print('I am thinking of a number between 1 and 20 ')
while guessestaken < 4:
    guess = int(raw_input('Take a guess and hit enter'))
    guessestaken = guessestaken + 1
    if guess < number:
        print('your number is too low')
    if guess > number:
        print('your number is too high ')
    if guess == number:
        print('well done the number was ' + number + ' and you got it in ' + guessestaken + '')
        break

Upvotes: 0

plamut
plamut

Reputation: 3206

You need to ask the user for input inside your loop, otherwise you are just comparing his first guess multiple times. Also, you should convert the input value to an integer, because raw_input returns a string.

>>> guess = raw_input('guess the number> ')
>>> type(guess)
<type 'str'>
>>> type(int(guess))
<type 'int'>

Upvotes: 0

Related Questions