石头剪刀布游戏,无限循环


import random
options = ['Rock','Paper', 'Scissor']
npc = random.choice(options)
print('Hello')
print('We are about to play Rock,Paper,Scissors.')
while True:
npc = random.choice(options)
player = str(input('Please declare your weapon: ')).capitalize()
if (player == npc):
print('Your choice: ', player)
print('npc choice: ', npc)
print('Oopsie looks like we have a tie!')
print('Lets Try again!')
continue
if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):
print('Poo Poo, that is not a valid option! Please try again!')
continue
if ((player == 'Rock') and (npc == 'Scissor')) or ((player == 'Scissor') and (npc == 'Paper')) or ((player == 'Paper') and (npc == 'Rock')):
print('Your choice: ', player)
print('npc choice: ', npc)
print('You win!')
break
if ((player == 'Rock') and (npc == 'Paper')) or ((player == 'Scissor') and (npc == 'Rock')) or ((player == 'Paper') and (npc == 'Scissor')):
print('Your choice: ', player)
print('npc choice: ', npc)
print('You lose!')
break

它不断打印出它是领带,不会显示任何其他结果。我刚刚开始编程。任何意见将不胜感激!

编辑:循环已解决。

这是根据请求的示例输出:

Output: Hello
We are about to play Rock,Paper,Scissors.
Please declare your weapon: rock
Your choice:  Rock
npc choice:  Paper
You lose!

此行

if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):

如果没有平局,将永远True。将其更改为

if player not in options:

改进代码的一些建议

您可以删除所有if中的()。这

if (player == npc):

if player == npc:

您还应该使用if/elif/else,而不仅仅是if。这将使continue的使用变得不必要。

编辑:改进版本:

import random
options = ['Rock','Paper', 'Scissor']
npc = random.choice(options)
print('Hello')
print('We are about to play Rock,Paper,Scissors.')
while True:
npc = random.choice(options)
player = str(input('Please declare your weapon: ')).capitalize()
if player == npc:
print('Your choice: ', player)
print('npc choice: ', npc)
print('Oopsie looks like we have a tie!')
print('Lets Try again!')
elif player not in options:
print('Poo Poo, that is not a valid option! Please try again!')
elif (player == 'Rock' and npc == 'Scissor') or (player == 'Scissor' and npc == 'Paper') or (player == 'Paper' and npc == 'Rock'):
print('Your choice: ', player)
print('npc choice: ', npc)
print('You win!')
break
else:
print('Your choice: ', player)
print('npc choice: ', npc)
print('You lose!')
break

您的程序中存在逻辑错误。

具体来说,这一行:

if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):

如果"or"运算符链接的语句中至少有一个为 True,则返回 True。

例如,假设玩家选择了"摇滚"。现在第一个语句player != 'Rock'是假的,但第二个语句player != 'Paper'是真,player != 'Scissor'也是。

因此,整个语句变为False or True or TrueTrue,程序最终告诉用户他们的选择无效。

您可以通过使用"and"而不是"or"来轻松解决此问题,如下所示:

if (player != 'Rock') and (player != 'Paper') and (player != 'Scissor'):

在这里,语句变为False and True and True即 False。仅当玩家输入的选项不是选项之一时,此语句才返回 True:Rock, Paper, Scissor,如预期的那样。

一种更 Python 式的方法是将整个语句替换为以下内容,如另一个答案中所述:

if player not in options:

我建议您进行一些改进,例如使用if-elif,而不是continue。还可以使用.format(...)来表示回复。

对于循环问题,将第二个 if 语句中的逻辑运算符更改为and运算符以进行独占迭代。

最终格式化的代码如下所示:

import random
options = ['Rock','Paper', 'Scissor']
npc = random.choice(options)
print('Hello')
print('We are about to play Rock,Paper,Scissors.')
while True:
npc = random.choice(options)
player = str(input('Please declare your weapon: ')).capitalize()
if (player == npc):
print('Your choice: {}'.format(player))
print('npc choice: {}'.format(npc))
print('Oopsie looks like we have a tie!')
print('Lets Try again!')
elif (player != 'Rock') and (player != 'Paper') and (player != 'Scissor'):
print('Poo Poo, that is not a valid option! Please try again!')
elif ((player == 'Rock') and (npc == 'Scissor')) or ((player == 'Scissor') and (npc == 'Paper')) or ((player == 'Paper') and (npc == 'Rock')):
print('Your choice: {}'.format(player))
print('npc choice: {}'.format(npc))
print('You win!')
break
elif ((player == 'Rock') and (npc == 'Paper')) or ((player == 'Scissor') and (npc == 'Rock')) or ((player == 'Paper') and (npc == 'Scissor')):
print('Your choice: {}'.format(player))
print('npc choice: {}'.format(npc))
print('You lose!')
break

最新更新