英文:
Is there any way to set 0 as value for while rather than false? (first week of python) (SOLVED)
问题
以下是我之前为了练习在Python中使用 while
循环所写的代码。它运行正常:
x = int(input("你的等待号码是多少?"))
while 1 <= x:
if 1 <= x <= 15:
print("顾客号码 {} 请取您的咖啡".format(x))
elif 15 < x <= 20:
y = 5*(x-15)
print("请再等待 {0} 分钟".format(y))
elif x > 20:
print("抱歉,今天结束了")
x = int(input("你的等待号码是多少?"))
print("错误。x 应该大于 0")
但后来,我想简化它并将最后的错误打印代码放在循环内。
所以我改变了 while
的条件为 x
本身(这样可以吗?),然后加了一个带有 print
和 break
代码的 else
行:
x = int(input("你的等待号码是多少?"))
while x:
if 1 <= x <= 15:
print("顾客号码 {} 请取您的咖啡".format(x))
elif 15 < x <= 20:
y = 5*(x-15)
print("请再等待 {0} 分钟".format(y))
elif x > 20:
print("抱歉,今天结束了")
else:
print("错误。x 应该大于 0")
break
x = int(input("你的等待号码是多少?"))
当 x 为正数或负数时,似乎这个方法也能正常工作。
但问题是,当我将 x 输入为 0 时,在 break
之前的最后一个打印代码不会运行。
我想到在 while
循环中,0 被视为假,因此 while x=0
会立即终止代码。
有没有办法将 0 设置为 while
的值,而不是 False
?
已解决
得到了答案,非常简单。
只需将 while
的条件设置为 True:
while True
这样它将在不受 x 值决定的情况下运行。
这样,x = 0 也将顺利运行。
英文:
Below is the code that I previously made for practicing while
code in Python. It was working fine
x = int(input("what is your waiting number? "))
while 1 <= x:
if 1<= x <=15:
print("customer number {} please get your coffee".format(x))
elif 15 < x <= 20:
y = 5*(x-15)
print("please wait for another {0} min".format(y))
elif 20 < x:
print("sorry. end for today")
x = int(input("what is your waiting number? "))
print("error. x should be bigger than 0")
But then, I wanted to simplify it and have the last print error code inside the while code.
So I've changed the condition for while to x
itself (is this ok?), then added an else line with the print
and break
code
x = int(input("what is your waiting number? "))
while x:
if 1<= x <=15:
print("customer number {} please get your coffee".format(x))
elif 15 < x <= 20:
y = 5*(x-15)
print("please wait for another {0} min".format(y))
elif 20 < x:
print("sorry. end for today")
else:
print("error. x should be bigger than 0")
break
x = int(input("what is your waiting number? "))
this seemed to work fine when x was either a positive or a negative value.
but the problem was, when I entered 0 as the value for x, the last print code before break
wouldn't run.
I figured that in a 'while' code, 0 is set to false, so while
x=0 will immediately terminate the code.
Is there any way to set 0 as a value for while
rather than False
?
SOLVED
Got an answer, was very simple.
just make the condition for while as True
while True
so that it will run without being decided by the value of x.
That case, x = 0 will also be running without problem.
答案1
得分: 0
Short answer: 不。
Long answer:
由于您有一个while x
循环,对于x=0
,代码会读取while 0
。只有在while
后面的内容求值为True
时,才会执行while
范围内的代码块。
大多数“空”的变量都会评估为False
,无论其类型是什么。一个空的string
、list
或dict
、0
、False
等等... 其结果为False
的操作也会评估为False
,例如1==3
。
例如:
1
、True
、20<23
、"foo"
、['bar', 2]
,甚至负数如-10
... 都会评估为True
。
0
、False
、23<20
、''
、[]
,在Python中都会评估为False
。
请注意,None
是一种不同的情况。它既不评估为True也不评估为False,但仍然不执行while
循环。
尝试执行以下代码:
# 这些将评估为True。
x=1
while x:
print("1")
break
x=True
while x:
print("True")
break
x=(20<23)
while x:
print("(20<23)")
break
x='foo'
while x:
print("'foo'")
break
x=['bar',2]
while x:
print("['bar',2]")
break
x=-10
while x:
print("-10")
break
# 这些将评估为False。
x=0
while x:
print("0")
break
x=False
while x:
print("False")
break
x=''
while x:
print("''")
break
x=[]
while x:
print("[]")
break
# 特殊情况:None。既不是True也不是False,但也不执行while循环。
x=None
while x:
print("None")
break
它只输出真值:
1
True
(20<23)
'foo'
['bar',2]
-10
英文:
Short answer : No.
Long answer :
Since you have a while x
loop, for x=0
, the code reads while 0
. code inside the while
scope is only executed if what comes after the word while
evaluates to True
.
Most 'empty' variables evaluate to False, no matter their type. An empty string
, list
or dict
, 0
, False
...
An operation whose result is False
also evaluates to False
, such as 1==3
.
For example :
1
, True
, 20<23
, "foo"
, ['bar', 2]
, even negative numbers such as -10
... all evaluate to True.
0
, False
, 23<20
, ''
, []
, all evaluate to False in python.
Note That None
is a different case. It doesn't evaluate to True or False, but still doesn't execute while
loops.
Try to execute the following code :
# These will evaluate to True.
x=1
while x:
print("1")
break
x=True
while x:
print("True")
break
x=(20<23)
while x:
print("(20<23)")
break
x='foo'
while x:
print("'foo'")
break
x=['bar',2]
while x:
print("['bar',2]")
break
x=-10
while x:
print("-10")
break
# These will evaluate to False.
x=0
while x:
print("0")
break
x=False
while x:
print("False")
break
x=''
while x:
print("''")
break
x=[]
while x:
print("[]")
break
# Special case : None. Neither True or False, but doesn't execute while loops either.
x=None
while x:
print("None")
break
It only outputs the truthy values :
1
True
(20<23)
'foo'
['bar',2]
-10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论