擅长:python、mysql、java
<p>您可以分开测试来检查输入是否是函数的正整数</p>
<pre><code>def is_positive(n):
"""(str) -> bool
returns True if and only if n is a positive int
"""
return n.isdigit()
</code></pre>
<p>接下来,您可以创建一个函数来请求一个正整数。为此,请避免使用<code>str.isnumeric</code>方法,因为它也为浮点返回<code>True</code>。而是使用<code>str.isdigit</code>方法。在</p>
^{pr2}$
<p><code>request_input</code>将永远循环,直到收到正整数。这些简单的模块可以组合起来实现您想要的。特别是您的情况:</p>
<pre><code>def get_user_inputs(prompt):
time_units = request_input("This is the number of time units to simulate > ")
num_atoms = request_input("How many atoms should we simulate ? ")
rad_breaker = request_input("The radius of the beaker is ? ")
return time_units, num_atoms, rad_breaker
</code></pre>