我讨厌提出重复的问题,但我整天都在研究和研究这个程序而没有太多运气。它应该在Cel中采取一定的温度。或远,以及英尺或米的高度,并告诉你在给定的高度,水是液体,气体还是固体形式(不考虑大气压力,我只是试图得到一个粗略的估计在高度上)。经验法则是,海水高度每隔300米(或1000英尺),水的沸度将比100摄氏度低1度。
我设法找到几种方法让它返回一个大致正确的数字。下一步是向程序添加错误检查。我尝试了一个'try'条款,但它没有发现错误,而对于我的生活,我无法弄清楚原因。
修改
我尝试了另一种方式,除了一个奇怪的问题之外,它可以捕捉错误。在两个函数inpALT()和inpTEMP中输入输入时,它要求我输入两次输入,然后返回正确的值...:
def inpALT():
alt = str(input("Enter altitude above altlevel, format 100M/F :"))
if re.match(r'[0-9]*[mMfF]$', alt):
return alt
else:
raise ValueError("Invalid Format")
def inpTEMP():
temp = str(input("Tempurature in format 70C/F :"))
if re.match(r'[0-9]*[cCfF]$', temp):
return temp
else:
raise ValueError("Invalid Format")
while True:
try:
inpALT()
break
except ValueError:("Invalid Format")
while True:
try:
inpTEMP()
break
except ValueError:("Invalid Format")
temp = inpTEMP()
alt = inpALT()
---- snip ----
但是,只有在我必须输入数据两次之后才会这样做:
输入高于altlevel的海拔高度,格式为100M / F:100F
以70C / F格式输入温度:100F
以70C / F格式输入温度:100F
输入高于altlevel的海拔高度,格式为100M / F:100F
为什么会这样做??
答案 0 :(得分:2)
这是您在评论中获得的帮助的摘要。我为您提供了针对您的具体问题的更正代码。我对它进行了评论,所以我希望通过自己阅读和实现它可以帮助很多。
我试图让您了解在while loop
内重新构建代码的想法。我不推荐提供的代码,但这不是重点。我不想太打破你的编码风格。我专注于你提供的第二个代码。对我来说,我不清楚我必须去哪里。
#!/usr/bin/env/python
# -*- coding: utf-8 -*-
"""corrected version with more PEP8 included."""
import re
import sys # just for exit the script. There are multiple other ways.
# the assignments will be done later, no need for that here.
# alt = ""
# temp = ""
# readable names are better than cryptic ones
def get_altitude():
alt = str(input("Enter altitude above sealevel, format 100M/F: "))
# after `return` code will not be executed. You have to do it before the
# return statement as mentioned by jojonas. Maybe you will also allow a
# minus at the beginning and disallow no numbers (+ instead of *)?
if not re.match(r'[0-9]*[mMfF]$', alt):
raise ValueError
return alt
# readable names are better than cryptic ones
def get_temperature():
temp = str(input("Tempurature in format 70C/F: "))
# the same here with the return statement
if not re.match(r'[0-9]*[cCfF]$', temp):
raise ValueError
return temp
# The while loop stands for: do it forever. Maybe this is not what you want?!
# The user has to give a wrong input format (then sys.exit is your friend as
# one example of stopping the script or the user has to manually stop the
# script (STRG+C) or something like that. There are better ways.
while True:
try:
# you need to assign the returned values to a variable as mentioned
# by jojonas already. Here is the way of doing it.
alt = get_altitude()
temp = get_temperature()
# slight change in catching the error
except ValueError:
print('Invalid input format!')
sys.exit(1)
# finally will be done after exception occured! If you don't use
# sys.exit (as I did as a fast hack not to totally disorder your
# program), you have to be careful about this solution.
finally:
t = ''.join(x for x in temp if x.isdigit())
a = ''.join(x for x in alt if x.isdigit())
t = int(t)
a = int(a)
if "F" in temp:
# just another way of expressing the same:
t -= 32 / 1.8
if "F" in alt:
# just another way of expressing the same:
a /= 3.3
tPoint = 100 - a * 0.00552
# just another way of expressing the same:
if 0 - (int(tPoint)) < a < (100 - (int(tPoint))):
print("Liquid")
if a < (0 - int(tPoint)):
print("Solid")
if a > (100 - int(tPoint)):
print("Gas")
我希望这会对你有所帮助。请问是否还有不清楚的事情。