[英]Python Variable Understanding
I was writing this code and I can't seem to get it to work correctly.我正在编写这段代码,但我似乎无法让它正常工作。 There's no Syntax errors so I'm clear on that.
没有语法错误,所以我很清楚。 It just is not giving me the correct output I want.
它只是没有给我我想要的正确的 output。
Here's the code:这是代码:
This is the out put I get when x = 5: Big!这是我在 x = 5 时得到的输出:大! Done!
完毕!
This is the output I get when x = 1 Small!这是我在 x = 1 Small 时得到的 output! Done!
完毕!
This is what I get when x = anything other than 1 or 5 Done!这就是当 x = 1 或 5 以外的任何值时我得到的结果 完成!
What I want it to do is when x = anything between 1-5 to output Small?我想要它做的是当 x = 1-5 到 output Small 之间的任何东西时? then Done!
然后完成! Or if it is between 5-infinity to output Big!
或者如果它在 5 无穷大到 output 之间大! then Done!
然后完成! but if the number is not 1 or 5 it just outputs Done!
但如果数字不是 1 或 5,它只会输出 Done! What changes should I do to my code?
我应该对我的代码进行哪些更改?
x = 5
if x in range(6):
print('Small')
elif x >=6 :
print('Big')
print('Done')
Try this.尝试这个。 The range function checks if the number is in between 0 and 6 ie 0 to 5. Anything lesser than 0 will be ignored
范围 function 检查数字是否在 0 到 6 之间,即 0 到 5。任何小于 0 的都将被忽略
The problem with your code is that you're just checking two conditions if x== 5
and if x == 1
.您的代码的问题在于您只是在检查两个条件
if x== 5
和if x == 1
。 The print statements will be executed only if this condition is satisfied.仅当满足此条件时才会执行打印语句。
Cheers干杯
As pointed by a user in the previous answer, what you need to implement is an if-else ladder and use logical operator for the case when your output is specifically either 1 OR 5正如用户在上一个答案中指出的那样,您需要实现的是 if-else 梯形图,并在 output 具体为 1 或 5 的情况下使用逻辑运算符
x=6 # You can replace this by a user defined input using input()
if x==5 or x==1:
print("Done!")
elif x<5:
print("Small!")
print("Done!")
elif x>5:
print("Big!")
print("Done!")
else:
print("Enter a valid number!")
I checked with various test cases like 1, 5, numbers between 1 and 5 and numbers greater than 5 and all seem to work fine.我检查了各种测试用例,例如 1、5、1 到 5 之间的数字以及大于 5 的数字,一切似乎都运行良好。
You can create a if-else
ladder to achieve this您可以创建一个
if-else
阶梯来实现此目的
def determine_size(inp):
if inp == 1 or inp == 5:
print("Done")
elif 0 <= inp < 5:
print("Small")
print("Done")
elif inp > 6:
print("Big")
print("Done")
else:
print("Negative Input")
>>> determine_size(0)
Small
Done
>>> determine_size(100)
Big
Done
>>> determine_size(60)
Big
Done
>>>
>>> determine_size(3)
Small
Done
>>> determine_size(4)
Small
Done
You can also play around with the if-else
statements per your objectives您还可以根据您的目标使用
if-else
语句
x = 40
# at first check if x is greater than 1 and less than 5.
# Only then it is between 1 and 5.
if x >=1 and x<=5:
print('Small!')
# Now chek if x is greater than 5
elif x>5:
print('Big!')
print('Done!')
if you meant the above mentioned scenario mentioned in comment如果您的意思是评论中提到的上述情况
try:
x=int(input("enter a number: "))
if x >=0 or x<=5:
print("Small")
print("Done")
elif x>=6:
print("big")
print("Done")
except ValueError:
print("Done")
here the block of code is written in try block, why i have written in try block if someone enter something which is not number then program will not crash and raise a exception
.这里的代码块写在 try 块中,为什么我写在 try 块中,如果有人输入不是数字的东西,那么程序不会崩溃并
raise a exception
。
if you are not familiar with elif syntax, we can have simple construction like this:-如果你不熟悉 elif 语法,我们可以有这样的简单构造:-
try:
x=int(input("enter a number: "))
if x >=0 and x<=5:
print("Small")
print("Done")
else:
print("big")
print("Done")
except ValueError:
print("Done")
Here all I have done is to change the logical operator from or
to and
.在这里,我所做的只是将逻辑运算符从
or
更改为and
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.