首先我很抱歉,因为我知道这是一个基本问题,我试图从其他类似问题中寻找线索以找到答案,但到目前为止我还没有解决我的问题。
错误出现在下面的最后一段代码中,我尝试添加一个功能来重新启动程序,以便用户可以继续玩。
错误是“user_win() 缺少 2 个必需的位置参数:'player' 和 'opponent'”
--
代码是:
import random
def play():
user = input("Enter 'rock', 'paper', or 'scissors': ").lower()
computer = random.choice(['rock', 'paper', 'scissors'])
return '\nThe computer randomly entered "{}".'.format(computer)
def user_win(player, opponent):
if (player == 'rock' and opponent =='scissors') or (player == 'scissors' and opponent == 'paper') or (player == 'paper' and opponent == 'rock'):
return 'Game result: you won!\n',
elif player == opponent:
return 'Game result: it\'s a tie!\n',
elif player is not 'rock' or 'paper' or 'scissors':
return 'Oops, enter a correct word.',
else:
return 'Game result: you lost!',
def script():
print(play())
print(user_win())
restart = input("Would you like to try again?")
if restart == "yes":
print(script())
if restart == "no":
print("Thank you for playing.")
我尝试在 print(user_win(player, opponent))
中输入 def script()
,然后我收到了“玩家”和“对手”未定义的错误。所以我尝试在 def script()
上方定义这些变量,但我不知道如何定义。
答案 0 :(得分:0)
您可以直接从 user_win(user, computer)
调用 play()
,因为 play 是定义每个玩家移动的地方。
答案 1 :(得分:0)
只需从 play()
返回用户和计算机输入并传递给 user_win(player, opponent)
也更新了 elif player not in ['rock','paper','scissors']:
代码:
import random
def play():
user = input("Enter 'rock', 'paper', or 'scissors': ").lower()
computer = random.choice(['rock', 'paper', 'scissors'])
print('\nThe computer randomly entered "{}".'.format(computer))
return user, computer
def user_win(player, opponent):
if (player == 'rock' and opponent =='scissors') or (player == 'scissors' and opponent == 'paper') or (player == 'paper' and opponent == 'rock'):
return 'Game result: you won!\n'
elif player == opponent:
return 'Game result: it\'s a tie!\n'
elif player not in ['rock','paper','scissors']:
return 'Oops, enter a correct word.'
else:
return 'Game result: you lost!'
def script():
player, opponent = play()
print(user_win(player,opponent))
restart = input("Would you like to try again?")
if restart == "yes":
print(script())
if restart == "no":
print("Thank you for playing.")
script()
结果:
Enter 'rock', 'paper', or 'scissors': rock
The computer randomly entered "paper".
Game result: you lost!
Would you like to try again?yes
Enter 'rock', 'paper', or 'scissors': paper
The computer randomly entered "scissors".
Game result: you lost!
Would you like to try again?
答案 2 :(得分:0)
在下面这部分代码中,当您调用 user_win() 时,您没有传递玩家和对手的参数,因此您会收到此错误。
def script():
print(play())
print(user_win())
相反,您可以这样做:
#modify the play section to return the choices
def play():
user = input("Enter 'rock', 'paper', or 'scissors': ").lower()
computer = random.choice(['rock', 'paper', 'scissors'])
return [computer,computer]
def script():
print(user_win(play()[0],play()[1]))
答案 3 :(得分:0)
#Friend 你没有在 user_win 中传递任何参数,这就是你收到 #the 错误的原因。
随机导入
def user_win(玩家,对手):
if (player == 'rock' and opponent == 'scissors') or (player == 'scissors' and opponent == 'paper') or (
player == 'paper' and opponent == 'rock'):
return 'Game result: you won!\n'
elif player == opponent:
return 'Game result: it\'s a tie!\n'
elif player != 'rock' or player != 'paper' or player != 'scissors':
return 'Oops, You lose.'
else:
return 'You entered a wrong word !'
定义脚本():
user = input("Enter 'rock', 'paper', or 'scissors': ").lower()
computer = random.choice(['rock', 'paper', 'scissors'])
print(f"'\nThe computer randomly entered : {computer} ")
print(user_win(user, computer))
restart = input("Would you like to try again? write yes(y)/ no(n) : ")
if restart == "yes" or restart == "y":
print(script())
elif restart == "no" or restart == "n":
print("Thank you for playing.")
exit()
if name == 'main':
script()