有没有办法将0设置为while的值,而不是false?(Python的第一周)(已解决)

huangapple go评论63阅读模式
英文:

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 本身(这样可以吗?),然后加了一个带有 printbreak 代码的 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(&quot;what is your waiting number? &quot;))

while 1 &lt;= x:
    
    if 1&lt;= x &lt;=15:
        print(&quot;customer number {} please get your coffee&quot;.format(x))
    
    elif 15 &lt; x &lt;= 20:
        y = 5*(x-15)
        print(&quot;please wait for another {0} min&quot;.format(y))
    
    elif 20 &lt; x:
        print(&quot;sorry. end for today&quot;)

    x = int(input(&quot;what is your waiting number? &quot;))

print(&quot;error. x should be bigger than 0&quot;)

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(&quot;what is your waiting number? &quot;))

while x:
    
    if 1&lt;= x &lt;=15:
        print(&quot;customer number {} please get your coffee&quot;.format(x))
    
    elif 15 &lt; x &lt;= 20:
        y = 5*(x-15)
        print(&quot;please wait for another {0} min&quot;.format(y))
    
    elif 20 &lt; x:
        print(&quot;sorry. end for today&quot;)
    
    else:
        print(&quot;error. x should be bigger than 0&quot;)
        break

    x = int(input(&quot;what is your waiting number? &quot;))

this seemed to work fine when x was either a positive or a negative value.

有没有办法将0设置为while的值,而不是false?(Python的第一周)(已解决)

but the problem was, when I entered 0 as the value for x, the last print code before break wouldn't run.

有没有办法将0设置为while的值,而不是false?(Python的第一周)(已解决)

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,无论其类型是什么。一个空的stringlistdict0False等等... 其结果为False的操作也会评估为False,例如1==3

例如:
1True20&lt;23&quot;foo&quot;[&#39;bar&#39;, 2],甚至负数如-10... 都会评估为True
0False23&lt;20&#39;&#39;[],在Python中都会评估为False

请注意,None是一种不同的情况。它既不评估为True也不评估为False,但仍然不执行while循环。

尝试执行以下代码:

# 这些将评估为True。

x=1

while x:
    print(&quot;1&quot;)
    break

x=True

while x:
    print(&quot;True&quot;)
    break

x=(20&lt;23)

while x:
    print(&quot;(20&lt;23)&quot;)
    break

x=&#39;foo&#39;

while x:
    print(&quot;&#39;foo&#39;&quot;)
    break

x=[&#39;bar&#39;,2]

while x:
    print(&quot;[&#39;bar&#39;,2]&quot;)
    break

x=-10

while x:
    print(&quot;-10&quot;)
    break

# 这些将评估为False。

x=0

while x:
    print(&quot;0&quot;)
    break

x=False

while x:
    print(&quot;False&quot;)
    break

x=&#39;&#39;

while x:
    print(&quot;&#39;&#39;&quot;)
    break

x=[]

while x:
    print(&quot;[]&quot;)
    break

# 特殊情况:None。既不是True也不是False,但也不执行while循环。

x=None

while x:
    print(&quot;None&quot;)
    break

它只输出真值:

1
True
(20&lt;23)
&#39;foo&#39;
[&#39;bar&#39;,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&lt;23, &quot;foo&quot;, [&#39;bar&#39;, 2], even negative numbers such as -10 ... all evaluate to True.
0, False, 23&lt;20, &#39;&#39;, [], 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(&quot;1&quot;)
	break
	
x=True

while x:
	print(&quot;True&quot;)
	break
	
x=(20&lt;23)

while x:
	print(&quot;(20&lt;23)&quot;)
	break
	
x=&#39;foo&#39;

while x:
	print(&quot;&#39;foo&#39;&quot;)
	break
	
x=[&#39;bar&#39;,2]

while x:
	print(&quot;[&#39;bar&#39;,2]&quot;)
	break

x=-10

while x:
	print(&quot;-10&quot;)
	break

# These will evaluate to False.	

x=0

while x:
	print(&quot;0&quot;)
	break
	
x=False

while x:
	print(&quot;False&quot;)
	break
	
x=&#39;&#39;

while x:
	print(&quot;&#39;&#39;&quot;)
	break
	
x=[]

while x:
	print(&quot;[]&quot;)
	break
	

# Special case : None. Neither True or False, but doesn&#39;t execute while loops either.	

x=None

while x:
	print(&quot;None&quot;)
	break

It only outputs the truthy values :

1
True
(20&lt;23)
&#39;foo&#39;
[&#39;bar&#39;,2]
-10

huangapple
  • 本文由 发表于 2023年6月6日 17:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定