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!发布于 2018-04-22 06:43:04
这条线
if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):如果没有领带的话,永远都是True。把它改成
if player not in options:一些改进代码的建议
您可以删除所有()s中的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发布于 2018-04-22 07:00:43
程序中有一个逻辑错误。
具体而言,这一行:
if (player != 'Rock') or (player != 'Paper') or (player != 'Scissor'):如果它链接的语句中至少有一个是True,则'or‘运算符返回True。
例如,假设玩家选择了“摇滚”。现在,第一个语句player != 'Rock'是假的,但是第二个player != 'Paper'是True,player != 'Scissor'也是。
因此,整个语句变成了False or True or True,它是True,程序最后告诉用户他们的选择无效。
您可以通过使用'and‘而不是’或‘之类的方法来轻松地修复这个问题:
if (player != 'Rock') and (player != 'Paper') and (player != 'Scissor'):在这里,语句变成False and True and True,这是假的。只有当玩家输入了一个不是选项之一的选择时,此语句才会返回True:Rock, Paper, Scissor,正如预期的那样。
要做到这一点,一个更重要的方法是将整个声明替换为以下内容,如另一个答复中所提到的那样:
if player not in options:发布于 2018-04-22 07:25:38
我建议您做一些改进,比如使用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!')
breakhttps://stackoverflow.com/questions/49963241
复制相似问题