Flo Chelsea
Flo Chelsea

Reputation: 11

Guess the number game Python

So, im new to python and i have this challenge: I have to make the Guess the Number game, between me and computer. So this is where im at so far.

import random


the_number = random.randint(1, 4)
guess = 0

print(the_number)
while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player guess lower...\n")
    elif guess < the_number:
        print("Player guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Player wins!")
        break


    guess = random.randint(1, 100)

    if guess > the_number:
        print("Computer guess lower...\n")
    elif guess < the_number:
        print("Computer guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")
    break


print("Thank you for playing")

I wanna know how to make this to not stop until one of us is right?

Upvotes: 0

Views: 5101

Answers (4)

Code With Faraz
Code With Faraz

Reputation: 62

# Import random library
import random
# Assign random value between 1-100
secret_number = random.randint(1, 100)
# get the user guess
guess = int(input("Guess the secret number between 1-100: "))

# Loop untill guess not equal to secret number
while guess != secret_number:
    if guess < secret_number:
        print("Sorry, your guess is too low.")
    else:
        print("Sorry, your guess is too high.")
    
    # guess the number again
    guess = int(input("Guess the secret number between 1-100: "))
 
# This statement will execute after coming out of while loop
print("You guessed the number!")

Upvotes: 2

anantdark
anantdark

Reputation: 435

To get the desired output, you just need to tweak the code a bit, the break in the if statement for the computer is out of the loop of else, so it will stop the execution no matter what.

To counter this condition, you can move the break inside the else block. By doing this, the program will only break if either CPU or you correctly guess the right number.

One more thing i wanted to point out is you are printing the selecting number in the program, if it is a guessing game, isn't it supposed to be in the back-end of the code. By entering the printed number, you can win the game in the first iteration.

And just to point out, your program is selecting the number from (1, 4) but the computer is set to guess the number from (1, 100). Kindly tick the answer if it is correct. The modified code is -

import random


the_number = random.randint(1, 4)
guess = 0

while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player guess lower...\n")
    elif guess < the_number:
        print("Player guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Player wins!")
        break

    guess = random.randint(1, 4)

    if guess > the_number:
        print("Computer guess lower...\n")
    elif guess < the_number:
        print("Computer guess higher...\n")
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")
        break


print("Thank you for playing")

Upvotes: 0

zenalc
zenalc

Reputation: 360

You can do something like this to make the computer smarter.

import random

the_number = random.randint(1, 4)
guess = 0

print(the_number)
minPossible = 0
maxPossible = 100

while guess != the_number:
    guess = int(input("Please enter a number: "))
    if guess > the_number:
        print("Player, guess lower...\n")
        if guess < maxPossible:
            maxPossible = guess - 1
    elif guess < the_number:
        print("Player, guess higher...\n")
        if guess > minPossible:
            minPossible = guess + 1
    else:
        print("Game Over! The number was", the_number, "The Player wins!")
        break

    guess = random.randint(minPossible, maxPossible)

    if guess > the_number:
        print("Computer, guess lower...\n")
        maxPossible = guess - 1
    elif guess < the_number:
        print("Computer, guess higher...\n")
        minPossible = guess + 1
    else:
        print("Game Over! The number was", the_number,"The Computer wins!")


print("Thank you for playing")

Upvotes: 1

Brenden Price
Brenden Price

Reputation: 537

Your problem is the break statement at the end of the while loop. The code is iterating through the loop one time, then the break is ending the loop.

Upvotes: 0

Related Questions