擅长:python、mysql、java
<p>为什么你要做一个<code>while True</code>,然后跳出这个循环,而你也可以把你的需求放在while语句中,因为你只想在你有了年龄后停止?</p>
<pre><code>age = None
while age is None:
input_value = input("Please enter your age: ")
try:
# try and convert the string input to a number
age = int(input_value)
except ValueError:
# tell the user off
print("{input} is not a number, please enter a number only".format(input=input_value))
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")
</code></pre>
<p>这将导致以下结果:</p>
<pre><code>Please enter your age: *potato*
potato is not a number, please enter a number only
Please enter your age: *5*
You are not able to vote in the United States.
</code></pre>
<p>这将起作用,因为年龄永远不会有一个没有意义的价值,代码遵循您的“业务流程”逻辑</p>