擅长:python、mysql、java
<p>您应该对<code>except</code>子句使用<code>else</code>,只有在<code>int(pressure)</code>正确运行时,它才会运行。此外,不要更改变量的类型;这很混乱:</p>
<pre><code>def func(str_pressure, unit):
try:
pressure = int(str_pressure)
except ValueError:
print("'{}' is not an integer".format(str_pressure))
else:
# `pressure` is assigned
if unit in {"pascal", "mbar", "atm", "torr"}:
print("pressure = {} {}".format(pressure, unit))
else:
print("'{}' is not an accepted unit".format(unit))
pressure, unit = input("Enter an integer and a unit(seperated by ,): ").split(',')
func(pressure, unit)
</code></pre>