Misbah Kareem
Misbah Kareem

Reputation: 1

Issue with simple Guess the number game in python

I have an issue with my simple guess the number game in python.The code is given below.The program never gives me a correct guess,it keep asking the number.

import random 
import time

time1 = time.time()

number = random.randint(1,1000)

print ("welcome to the guessing game")
name = input("what is your name? ")
print("well, " + name + " iam  thinking of the number between 1 and 1000")

while True:
guess = int(input("guess: ") )
if guess > number:
    print("too high!")
    if guess < number:
        print("too low!")
        if guess == number:
            break
        print("yahoo,you guessed the number!")
        input()
        time2 = time.time()

that is number guessing game in python 3.

Upvotes: 0

Views: 353

Answers (4)

Vicky Cp
Vicky Cp

Reputation: 11

import random as rand

# create random number
r =rand.randint(0,20)
i=0
l1=[]
while(i<4):enter code here
    number = int(input("guess the number :  "))
    if(number in l1):
        print("this number is alraedy entered")
        i=i
    else:
        l1.append(number)
        if(number == r):
            print(number)
            break
        if(number>r):
            print(" number is less than your number ")
        elif(number<r):
            print("number is greater than your number")
        i =i+1
print("number is")
print(r)

Upvotes: 0

captainsameer
captainsameer

Reputation: 55

secret_number = 5
chance = 1
while chance <= 3:
    your_guess = int(input("Your Guess:- "))
    chance = chance + 1
    if your_guess == secret_number:
        print("You Won !!")
        break
else:
    print("You failed..TRY AGAIN..")

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

You need to indent the code correctly, you should also use if/elif's as guess can only be one of higher, lower or equal at any one time. You also need to print before you break on a successful guess:

while True:
    guess = int(input("guess: ") )
    if guess > number:
        print("too high!")
    elif guess < number:
        print("too low!")
    elif guess == number:
        print("yahoo,you guessed the number!")
        time2 = time.time()
        break

There is no way your loop can break as your if's are nested inside the outer if guess > number:, if the guess is > number then if guess < number: is evaluated but for obvious reasons that cannot possibly be True so you loop infinitely.

Upvotes: 2

taesu
taesu

Reputation: 4580

import random 
import time

time1 = time.time()
number = random.randint(1,1000)

print ("welcome to the guessing game")
name = input("what is your name? ")
print("well, " + name + " i am  thinking of the number between 1 and 1000")

while True:
    guess = int(input("guess: ") )
    if guess > number:
        print("too high!")
    if guess < number:
        print("too low!")
    if guess == number:
        print("yahoo,you guessed the number!")
        time2 = time.time()
        break

without changing too much, here is a working code.

Upvotes: 0

Related Questions