简体   繁体   English

错误:缺少 2 个必需的位置参数

[英]Error: missing 2 required positional arguments

first of all I apologize as I know this is a basic question and I tried to find clues from other similar questions to find an answer but I haven't managed to solve my problem so far.首先,我深表歉意,因为我知道这是一个基本问题,我试图从其他类似问题中寻找线索以找到答案,但到目前为止我还没有解决我的问题。

The error appeared in the last block of code below, where I tried adding a functionality to restart the program so the user can keep playing.错误出现在下面的最后一段代码中,我尝试添加一个功能来重新启动程序,以便用户可以继续玩。

The error is "user_win() missing 2 required positional arguments: 'player' and 'opponent'"错误是“user_win() 缺少 2 个必需的位置参数:‘玩家’和‘对手’”

-- ——

Code is:代码是:

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

I tried entering print(user_win(player, opponent)) inside def script() and then I got the error that "player" and "opponent" are not defined.我尝试在def script()输入print(user_win(player, opponent))然后我得到了“玩家”和“对手”未定义的错误。 So I tried defining these variables above def script() but I didn't know how.所以我尝试在def script()之上定义这些变量,但我不知道如何定义。

你可以从play()调用user_win(user, computer) play()因为 play 是定义每个玩家的动作的地方。

Just return the user and computer input from play() and pass to user_win(player, opponent) also updated elif player not in ['rock','paper','scissors']:只需从play()返回用户和计算机输入并传递给user_win(player, opponent)也更新了elif player not in ['rock','paper','scissors']:

Code:代码:

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()

Result:结果:

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?

In this section of your code below, when you call user_win(), you're not passing the arguments for player and opponent so you're getting this error.在下面代码的这一部分中,当您调用 user_win() 时,您没有传递玩家和对手的参数,因此您会收到此错误。

def script():
    print(play())
    print(user_win())

instead, you could do this:相反,你可以这样做:

#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]))

#Friend you haven't passed any arguments in user_win you that's why you are getting #the error. #Friend 你没有在 user_win 中传递任何参数,这就是你收到 #the 错误的原因。

Here are some modifications in your code, now you can easily play your game.这里对您的代码进行了一些修改,现在您可以轻松玩游戏了。

import random随机导入

def user_win(player, opponent): 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 !'

def script():定义脚本():

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 ':如果名称== ' main ':

script()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM