gpt4 book ai didi

Python,tictactoe,语法错误

转载 作者:行者123 更新时间:2023-12-01 03:46:53 24 4
gpt4 key购买 nike

我是 Python 新手。我发现这个网站 TheHelloWorldProgram.com 教我如何编写一个非常容易理解的井字游戏,但我遇到了一些问题。我不断收到此语法错误消息“无效语法”,它指向player = False。它突出显示了“玩家”。为什么?我想不通。谢谢您的帮助。下面你将看到游戏的代码,请原谅#comments。它们是给我自己的:

#importing randint from the random module
from random import randint

#create a list of play options
1 = ['Rock', 'Paper', 'Scissors']

#assign a random play to the computer
computer = t[randint(0,2)]

#set player to False
player = False

while player == False:
#set player to True
player = input("Rock, Paper, Scissors?")

#if player = computer, it is a Tie!
if player == computer:
print("Tie!")

#else if player = ROCK
elif player == "Rock":
#computer = paper
if computer == "Paper":
#player lost, paper beats rock
print("You lose!", computer, "covers", player)
else:
#player win, rock beats scissors
print("You win!", player, "smashes", computer)

#else if player = PAPER
elif player == "Paper":
#if computer = scissors
if computer == "Scissors":
#player lost, scissors beats paper
print("You lose!", computer, "cut", player)
else:
#player win, paper beats rock
print("You win!", player, "covers", computer)

#else if player = SCISSORS
elif player == "Scissors":
if computer == "Rock":
print("You lose!", computer, "smashes", player)
else:
print("You win!", player, "cut", computer)
else:
print("That's not a valid play. Check your spelling!"

#player was set to True, but we want it to be False so the loop continues
player = False
computer = t[randint(0,2)]

最佳答案

1 = ['rock',etc] 应该是 t =供引用 python(我认为大多数语言)不接受 # 作为变量名你可以做 t1, t_1, t, one,但不能做 1t, 1_t,。所以数字和以数字开头的变量。正如@Wickramaranga 指出的。

正如@Jim 指出的那样,关闭您的打印语句。检查 python 错误的一个好地方是错误指向的语句之前的语句。例如,

print("That's not a valid play.  Check your spelling!"

#player was set to True, but we want it to be False so the loop continues
player = False

你的错误指向player = False,因为当python解释器执行时

print("That's not a valid play.  Check your spelling!"

它没有看到结尾“)”。

关于Python,tictactoe,语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com