"或"引起“if”的问题的条件 [英] "or" condition causing problems with "if"

查看:124
本文介绍了"或"引起“if”的问题的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数中遇到条件时遇到问题。 if 语句将评估为 True ,无论值 choice 是。当我删除时,如果正常工作。

I'm having trouble with an or condition in a function. The if statement keeps evaluating as True no matter what value choice is. When I remove the or, the if works correctly.

def chooseDim ():
    **choice = input ('Do you need to find radius or area? ')
    if choice == 'A' or 'a':**
        area = 0
        area = int(area)
        areaSol ()

    elif choice == 'R' or 'r':

        radSol ()

    else:
        print ('Please enter either A/a or R/r.')
        chooseDim ()


推荐答案

关于本身是正确的。你真的在问a是否为True,它总是如此。但是还有另一种方法:

The answers about or itself are correct. You're literally asking if "a" is True, which it always is. But there's an alternative approach:

if choice in 'Aa':

然后再次,没有任何问题:

Then again, there's nothing wrong with:

if choice.lower() == 'a':

这篇关于"或"引起“if”的问题的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆