3 回答

TA貢獻1831條經驗 獲得超4個贊
您應該修改 cpu_choice 函數以返回一個值。此外,還應刪除您的 return 語句,如其旁邊的評論中所述:
def cpu_choice(): #This function is for the computer to get a option.
list_option = [1 , 2, 3]
cpu = random.choice(list_option)
#return(random.choice(list_option)) #This returns a number, but in your compare code you are comparing strings, so take this line out
if cpu == 1:
cpu = "Rock"
if cpu == 2:
cpu = 'Paper'
if cpu == 3:
cpu = 'Scissor'
return cpu
在主函數中,您可以將另一個名為 cpu 的變量設置為名為 cpu_choice 的函數的返回值
def main(cpu,get): #Executes the programs and checks to see if the input is valid.
print('Rock = 1')
print('Paper = 2')
print('Scissor = 3')
again = 'y'
while get < 1:
get = int(input('Enter a valid number.'))
while get > 3:
get= int(input('Enter a valid number.'))
if get == 1:
get = "Rock"
if get == 2:
get = 'Paper'
if get == 3:
get = 'Scissor'
cpu = cpu_choice()
compare(cpu,get)

TA貢獻1841條經驗 獲得超3個贊
您的 cpu_choice 應如下所示:
def cpu_choice():#This function is for the computer to get a option.
list_option = [1 , 2, 3]
cpu = random.choice(list_option)
if cpu == 1:
cpu = "Rock"
if cpu == 2:
cpu = 'Paper'
if cpu == 3:
cpu = 'Scissor'
return cpu
這是因為 return 將退出函數,因此函數中 return 語句后面的任何代碼將永遠不會被執行。
您的主要功能應如下所示:
def main(cpu,get):# Executes the programs and checks to see if the input is valid.
print('Rock = 1')
print('Paper = 2')
print('Scissor = 3')
again = 'y'
while get < 1:
get = int(input('Enter a valid number.'))
while get > 3:
get= int(input('Enter a valid number.'))
if get == 1:
get = "Rock"
if get == 2:
get = 'Paper'
if get == 3:
get = 'Scissor'
compare(cpu,get)
您不需要在主函數中聲明 cpu,因為您已經將 cpu 傳遞給了主函數。
在您的功能之外,您將需要這個:
get = int(input('Enter a number 1 to 3 as your choice.\n'))
cpu = cpu_choice()
main(cpu,get)
現在您的 main 函數具有它需要的所有參數。注意我放在get = int(input('Enter a number 1 to 3 as your choice.\n'))你的函數聲明之后。這是一種常見的做法,可以讓您更輕松地理解您的代碼。
質量優化
Pythonrandom可以從列表中選擇一個隨機元素:
Or's 可elif用于在您贏時為 1,如果您輸了則為 1。
考慮到您main()從內部調用compare()最好main()沒有參數,而是在主函數中獲取get和cpu
一個while語句可以有多個比較。
優化后的代碼如下所示:
import random
def cpu_choice():
list_option = ["Rock" , "Paper", "Scissor"]
cpu = random.choice(list_option)
return cpu
def compare(cpu,get):
if get == cpu:
print('Its a tie!')
again = input('Enter Y or y to play again. Enter N or n to quit.')
if again == 'y' or again == 'Y':
main()
elif cpu == 'Rock' and get == 'Scissor' or cpu == 'Paper' and get == 'Rock' or cpu == 'Scissor' and get == 'Paper':
print('You lose.')
again = input('Enter Y or y to play again. Enter N or n to quit.')
if again == 'y' or again == 'Y':
main()
elif cpu == 'Rock' and get == 'Paper' or cpu == 'Paper' and get == 'Scissor' or cpu == 'Scissor' and get == 'Rock':
print('You win!')
again = input('Enter Y or y to play again. Enter N or n to quit.')
if again == 'y' or again == 'Y':
main()
def main():
print('Rock = 1')
print('Paper = 2')
print('Scissor = 3')
get = int(input('Enter a number 1 to 3 as your choice.\n'))
cpu = cpu_choice()
while not 4 > get > 0:
get = int(input('Enter a valid number.'))
if get == 1:
get = "Rock"
if get == 2:
get = 'Paper'
if get == 3:
get = 'Scissor'
compare(cpu,get)
main()

TA貢獻1816條經驗 獲得超4個贊
在底部,您使用參數“cpu”(未定義)和“get”(由頂部的用戶輸入定義)調用 main。該程序接受 1 個輸入并打印一個輸出 - 您不需要將 cpu 參數提供給 main,因為它是從 cpu_choice 函數返回的內容生成的。只需將其作為參數刪除并在調用 compare 之前寫入 cpu = cpu_choice() ,并讓 cpu_choice() 返回 cpu 值。
添加回答
舉報