Python 3.x 猜一个数字程序,计算机和用户轮流猜。当我运行它时,我得到了这个

Python 3.x 猜一个数字程序,计算机和用户轮流猜。当我运行它时,我得到了这个,python-3.x,Python 3.x,以下代码我不确定是否完全正确,但就逻辑而言几乎是正确的: 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

以下代码我不确定是否完全正确,但就逻辑而言几乎是正确的:

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.")
当我运行它时,程序应该告诉我是否需要在计算机猜测的同时猜测更高或更低,但它只是不断地询问一个数字

while True:
    player_guess = int(input("Take a guess: "))
这两条线就是问题所在。Python是关于缩进的,缩进的代码行表示player_guess=在while循环中,因为True总是True,所以player_guess=行将永远运行。尝试更改它,使所有行缩进如下:

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.")
为True时:
player_guess=int(输入(“猜一猜:”)
如果玩家猜>号码:
打印(“玩家猜低…\n”)
如果玩家猜<号码:
打印(“玩家猜测更高…\n”)
如果玩家猜==玩家编号:
打印(“游戏结束!号码是”,号码是,“玩家获胜”)
sys.exit()
如果玩家猜>号码:
comp_guess=random.randint(1,玩家猜)
打印(“计算机的猜测是:”,comp_guess)
打印(“计算机猜测下限…\n”)
如果玩家猜<号码:
comp_guess=random.randint(玩家猜,100)
打印(“计算机的猜测是:”,comp_guess)
打印(“计算机猜测更高…\n”)
比较次数+=1
如果comp_guess==编号:
打印(“计算机的猜测是:”,comp_guess)
打印(“游戏结束!数字是”,数字是,“电脑赢了”。)
sys.exit()
输入(“\n\n按enter键退出。”)
或者,您可以不使用sys.exit()而直接执行
break

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.")