简体   繁体   English

基本的Python If语句使用“或”

[英]Basic Python If Statements Using “or”

I am following a beginner program for learning python. 我正在关注学习python的初学者程序。 I am struggling to find a better way to use an "if" statement with multiple possibilities. 我正在努力寻找一种更好的方法来使用具有多种可能性的“ if”语句。 I am writing a basic text based game in which the user is able to choose three different difficulty levels: Easy, Medium, Hard. 我正在写一个基于文本的基本游戏,用户可以选择三种不同的难度级别:简易,中,硬。 I am just trying to write code that takes in a 1, 2, or 3 to tell the program which level to run. 我只是试图编写采用1、2或3的代码来告诉程序要运行的级别。 I want to make sure the user input is one of those options, not "4" or "one". 我想确保用户输入是这些选项之一,而不是“ 4”或“一个”。

Here is the relevant code I have written: 这是我编写的相关代码:

selected_level = 0
selected_level = raw_input("\n Please select a level by entering 1, 2, or 3 : ")
print selected_level
if selected_level == 1 or selected_level == 2 or selected_level == 3:
    print "Pass"
else:
    print "Fail"
break

All I am trying to do right now is test that the "if" statement works. 我现在要尝试做的就是测试“ if”语句是否有效。 However, no matter what input I enter it will print the input and then print "Fail" which tells me it is never entering the first part of the "if" statement. 但是,无论我输入什么输入,它都会打印输入,然后打印“ Fail”,这告诉我它永远不会输入“ if”语句的第一部分。

I have tried testing this with inputs 1, 2, 3, 4, one, etc and it always skips to the "else" statement. 我尝试使用输入1、2、3、4、1等进行测试,但它总是跳到“ else”语句。 Is there a better way to write an "if" statement with multiple or values? 有没有更好的方法编写带有多个或值的“ if”语句?

Your issue is that raw_input returns a string, but the if-statement is comparing to ints. 您的问题是raw_input返回一个字符串,但是if语句正在与ints进行比较。

"1" does not equal 1. “ 1”不等于1。

Try checking if selected_level == "1" ... "2"... etc. and it should work. 尝试检查if selected_level == "1" ... "2"... etc. ,它应该可以工作。

I would compare to strings instead of casting the input to an int, as that will crash the program if the user types something unexpected. 我将比较字符串而不是将输入强制转换为int,因为如果用户键入意外的内容,这将使程序崩溃。

Also for a more succinct check: 也可以进行更简洁的检查:

if selected_level in ("1", "2", "3"):
    print "Pass"

raw_input returns a string. raw_input返回一个字符串。 Try int(raw_input(...)) 尝试int(raw_input(...))

The reason it keeps skipping down to the if statement is because it is comparing the string that you entered to the numbers 1, 2, etc and not to the string value of 1 and 2. If you want to take the numeric value of the input you could do something like: 之所以一直跳到if语句的原因是因为它会将输入的字符串与数字1、2等进行比较,而不是与字符串值1和2进行比较。如果要获取输入的数字值您可以执行以下操作:

selected_level = int(raw_input("\\n Please select a level by entering 1, 2, or 3 : "))

This will take the integer value of whatever the user inputs. 这将采用用户输入的整数值。

raw_input (in Python 2.x) returns a str . raw_input (在Python 2.x中)返回str so selected_level is a str , not int . 所以selected_level是一个str ,而不是int You must parse it to int before compare it woth int values 您必须先将其解析为int,然后再比较int

selected_level = 0
selected_level = int(input("\n Please select a level by entering 1, 2, or 3 : "))
print selected_level
if selected_level == 1 or selected_level == 2 or selected_level == 3:
    print "Pass"
else:
    print "Fail"
break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM