簡體   English   中英

錯誤:缺少 2 個必需的位置參數

[英]Error: missing 2 required positional arguments

首先,我深表歉意,因為我知道這是一個基本問題,我試圖從其他類似問題中尋找線索以找到答案,但到目前為止我還沒有解決我的問題。

錯誤出現在下面的最后一段代碼中,我嘗試添加一個功能來重新啟動程序,以便用戶可以繼續玩。

錯誤是“user_win() 缺少 2 個必需的位置參數:‘玩家’和‘對手’”

——

代碼是:

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

我嘗試在def script()輸入print(user_win(player, opponent))然后我得到了“玩家”和“對手”未定義的錯誤。 所以我嘗試在def script()之上定義這些變量,但我不知道如何定義。

你可以從play()調用user_win(user, computer) play()因為 play 是定義每個玩家的動作的地方。

只需從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?

在下面代碼的這一部分中,當您調用 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]))

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

如果名稱== ' main ':

script()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM