【发布时间】:2014-03-06 15:25:02
【问题描述】:
我已经完成了这项工作,但现在我无法弄清楚我做了什么使它无法按照我想要的方式运行。当程序被 pyshell 调用时,它会按应有的方式提出问题,但是当给出答复时,这就是它失去理智的地方。它正在工作,并会调用我定义的函数,但现在它不会这样做。
我可能只是将响应重新定义为使用数字而不是字符串,以便更轻松、更快地完成条目,但目前它不起作用,所以在我弄明白之前没有必要这样做。
错误在run_time() 部分,它没有按应有的方式调用函数,我可能遗漏了一些东西。我希望它在给出正确响应时调用该函数。
我试过了:
k = (query())
f = function_list[k]
f()
这也不值得,所以我被困住了。感谢您的帮助。
def f_to_c():
temp = int(input("How warm is it out?"))
f_or_c = str(input("Is it C or F?"))
if f_or_c == 'c' or f_or_c == 'C':
updatedtemp = float(temp * 9 / 5 + 32)
print (updatedtemp, 'F')
elif f_or_c == 'f' or f_or_c == 'F':
updatedtemp = float((temp - 32) * 5 / 9)
print (updatedtemp, 'C')
def calc_trapezoid_area():
height = float(Input('What is the height of the trapezoid?'))
length1 = float(Input('What is the length of the bottom base?'))
length2 = float(Input('What is the length of the top base?'))
formula = float((1 / 2 * (length1 + length2)) * height)
print ("The area of the trapezoid is:", (formula))
def cal_circle():
pi = float(245850922 / 78256779)
rad_or_diam = str(input('Diameter or Radius?'))
width = float(input('What is the %s of your Circle?' % rad_or_diam))
if rad_or_diam == 'r' or rad_or_diam == 'R':
print (float(pi * width ** 2))
elif rad_or_diam == 'd' or rad_or_diam == 'D':
print (float(pi * width))
def query():
query = str(input('What would you like to calculate? Degrees, Trapezoids, or Circles?')).lower
function_list = {
"degrees": f_to_c,
"trapezoids": calc_trapezoid_area,
"circles": cal_circle
}
def run_time():
try:
f = function_list[query()]
f()
except KeyError:
print ("You messed up!")
if __name__ == '__main__':
while True:
run_time()
x = str(input('True or False?'))
if x == 'True':
break
【问题讨论】:
-
I broke it and I can't figure out where I went wrong -
没看到那个,不是那个问题。它不会抛出错误,因为它甚至没有那么远。它确实从 run_time 获取输入并将其与条目匹配,然后调用该函数。现在它不会那样做,也不会发出警报。所以这就是我被卡住的原因。
标签: python python-3.x calculator