本文介绍了如果语句始终为真(字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个相当简单的石头,剪刀,剪子程序,在使用if语句时遇到了一些麻烦.由于某些原因,当我输入石头,纸张或剪刀(真值")时,程序始终会执行

I have here a rather simple rock, paper, scissors program where I am having some trouble with if statements. For some reason, when I enter rock, paper, or scissors (True Values), the program always performs

if 'rock' or 'paper' or 'scissors' not in player:
   print("That is not how you play rock, paper, scissors!")

由于某种原因.完整的程序如下.

for some reason. The complete program is as below.


computer = ['rock', 'paper', 'scissors']

com_num = randint(0,2)

com_sel = computer[com_num]

player = input('Rock, paper, scissors GO! ')
player = player.lower()

if 'rock' or 'paper' or 'scissors' not in player:
    print("That is not how you play rock, paper, scissors!")

if player == 'rock' or 'paper' or 'scissors':
    #win
    if player == 'rock' and com_sel == 'scissors':
        print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
    if player == "paper" and com_sel == "rock":
        print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')
    if player == 'scissors' and com_sel == 'paper':
        print('You win! ', player.title(), ' beats ', com_sel, '!', sep ='')

    #draw
    if player == com_sel:
        print('It\'s a draw!')

    #lose
    if player == 'rock' and com_sel == 'paper':
        print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
    if player == 'paper' and com_sel == 'scissors':
        print('You lose.', com_sel.title(), "beats", player, '!', sep = '')
    if player == 'scissors' and com_sel == 'rock':
        print('You lose.', com_sel.title(), "beats", player, '!', sep = '')```

推荐答案

if 中的条件错误.

考虑 if 语句并加上括号:

if ('rock') or ('paper') or ('scissors' not in player):

它将始终返回 True ,因为 rock 始终为true.

It will always return True because rock will always be true.

您需要交换条件的操作数

You need to swap conditions' operands

if player not in computer:

此交换之后,此行变得无关紧要(并且其条件也是错误的),您需要将其删除:

After this swap, this line becomes irrelevant (and also its conditions are wrong) You need to remove it:

if player == 'rock' or 'paper' or 'scissors':

这篇关于如果语句始终为真(字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 22:39