石头剪刀布没有正确执行获胜条件



我想这是我犯的一个愚蠢的错误。所以无论如何,我从来没有在我的代码中使用过多个orand,我在处理数据时开始使用它,但我犯了很多错误,所以我决定用它运行一个石头剪刀布程序,如果程序没有正确运行获胜条件就退出了。这是我的代码,感谢任何帮助!!

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

你忘了打印了

当你使用return时,它给函数赋值而不是打印它

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

您忘记更改外部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())

最新更新