非常快的問題,我認為這是我犯的一個愚蠢的錯誤。所以無論如何我從來沒有真正使用過multiple or,and在我的代碼中,我在處理資料時開始使用它,但我犯了很多錯誤,所以我決定使用它運行一個石頭剪刀布程式,它在沒有正確運行win的情況下退出程式健康)狀況。這是我的代碼,不勝感激!
import random
def isWin(player,opponent):
if (player=='p' and opponent=='r') or (player=='r' and opponent=='s') or (player=='s' and opponent=='p'):
return True
else:
return False
def play_rps():
resp='c'
while resp.lower()!='n':
user='x'
while user not in ['r','s','p']:
user = input("'r' for rock 'p' for paper 's' for scissors ")
computer = random.choice(['r', 's', 'p'])
print(f'You chose {user} and Computer chose: {computer} ')
if computer == user:
return 'Draw'
elif isWin(user, computer):
return 'You win!'
return 'You lose!'
print(play_rps())
uj5u.com熱心網友回復:
你忘記列印了。
當您使用 areturn時,它會為函式提供或分配一個值而不是列印它
import random
def isWin(player,opponent):
if (player=='p' and opponent=='r') or (player=='r' and opponent=='s') or (player=='s' and opponent=='p'):
return True
else:
return False
def play_rps():
user=''
user=input("'r' for rock 'p' for paper 's' for scissors ")
computer=random.choice(['r','s','p'])
if computer==user:
return 'Draw'
elif isWin(user,computer):
return 'You win!'
return 'You lose!'
print(play_rps())
uj5u.com熱心網友回復:
您忘記更改 external 的條件while loop,因此它進入無限回圈。
這應該可以解決問題
import random
def isWin(player,opponent):
if (player=='p' and opponent=='r') or (player=='r' and opponent=='s') or (player=='s' and opponent=='p'):
return True
else:
return False
def play_rps():
resp='c'
while resp.lower()!='n':
user='x'
while user not in ['r','s','p']:
user = input("'r' for rock 'p' for paper 's' for scissors ")
computer = random.choice(['r', 's', 'p'])
print(f'You chose {user} and Computer chose: {computer} ')
recp = 'n'
if computer == user:
return 'Draw'
elif isWin(user, computer):
return 'You win!'
return 'You lose!'
print(play_rps())
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491237.html
上一篇:有和沒有'else'的區別
