在我的理解中while True:永远的平均循环,like saying while True == True。
还有一种机制可以使用break语句打破无限循环。
我在the cs50 module中遇到一个函数,它使用while循环(持续提示输入int),其中没有中断。
我试图理解,当我们输入一个int时,循环中的“True语句”会变成False(我就是这么理解它的),然后停止提示用户。在
换句话说,在代码中什么可以将whiletrue改为false?在
其功能如下:def get_long(prompt=None):
"""
Read a line of text from standard input and return the equivalent long;
if text does not represent a long, user is prompted to retry. If line
can't be read, return None.
"""
while True:
s = get_string(prompt)
if s is None:
return None
if re.search(r"^[+-]?\d+$", s):
try:
return long(s, 10)
except ValueError:
pass
# temporarily here for backwards compatibility
if prompt is None:
print("Retry: ", end="")
内部使用的get_string()函数如下:
^{pr2}$
当我把一个字符串转换成一个真的字符串时,如何才能得到一个字符串呢?
有人能给我解释一下吗,也许我遗漏了while True:语句。在