Parker Deines
Parker Deines

Reputation: 3

Making a guess the number program where the computer and user take turns guessing. When Ever I run it i get this

Here's the code which i'm not sure is completely sound but is almost correct as far as logic goes:

import random

print("Welcome to the two player guess game!")

print("A number between 1 and 100 will be chosen at random, you and the computer must try to guess it!")
print("Try to guess it in as few attempts as possible!\n")

while True:
    player_guess = int(input("Take a guess: "))
if player_guess > the_number:
    print("Player guess lower...\n")
if player_guess < the_number:
    print("Player guess higher...\n")
if player_guess == the_number:
    print("Game Over! The number was", the_number, "The Player wins.")
sys.exit()

if player_guess > the_number:
    comp_guess = random.randint(1, player_guess)
print("The computer's guess was:", comp_guess)
print("Computer guess lower...\n")
if player_guess < the_number:
    comp_guess = random.randint(player_guess, 100)
print("The computer's guess was:", comp_guess)
print("Computer guess higher...\n")
comp_tries += 1
if comp_guess == the_number:
    print("The computer's guess was:", comp_guess)
print("Game Over! The number was", the_number, "The Computer wins.")
sys.exit()


input("\n\nPress the enter key to exit.")

Here's what happens when I run the code:

    Welcome to the two player guess game!
A number between 1 and 100 will be chosen at random, you and the computer must try to guess it!
Try to guess it in as few attempts as possible!

Take a guess: 11
Take a guess: 12
Take a guess: 13
Take a guess: 

When ever I run it, the program is supposed to tell me whether or not I need to guess higher or lower all while the computer guesses but instead it just keeps asking for a number.

Upvotes: 0

Views: 279

Answers (1)

Tim Lee
Tim Lee

Reputation: 358

while True:
    player_guess = int(input("Take a guess: "))

These two lines are the problem. Python is all about indents and the indented line of code that says player_guess = is within the while loop and since True is always True the player_guess = line is going to run forever. Try changing it so that you indent all the lines like so:

while True:
    player_guess = int(input("Take a guess: "))
    if player_guess > the_number:
        print("Player guess lower...\n")
    if player_guess < the_number:
        print("Player guess higher...\n")
    if player_guess == the_number:
        print("Game Over! The number was", the_number, "The Player wins.")
        sys.exit()

    if player_guess > the_number:
        comp_guess = random.randint(1, player_guess)
        print("The computer's guess was:", comp_guess)
        print("Computer guess lower...\n")
    if player_guess < the_number:
        comp_guess = random.randint(player_guess, 100)
        print("The computer's guess was:", comp_guess)
        print("Computer guess higher...\n")
        comp_tries += 1
    if comp_guess == the_number:
        print("The computer's guess was:", comp_guess)
        print("Game Over! The number was", the_number, "The Computer wins.")
        sys.exit()


    input("\n\nPress the enter key to exit.")

Or instead of sys.exit() You could just do break

Upvotes: 1

Related Questions