[英]This Python code for a Custom Calculator was working now I broke it and I can't figure out where I went wrong
I had this working and now I cannot figure out what I did to make it not function the way I intend it to. 我已经完成了这项工作,现在我无法弄清楚我做了什么,以使其无法按预期的方式运行。 When the program is called by the pyshell it asks the questions as it is supposed to but when the reply is given this is where it loses it mind.
当pyshell调用该程序时,它会按预期的方式询问问题,但是当给出答复时,它就会失去理智。 It was working and would call the function I have defined but now it will not do so.
它正在工作,可以调用我定义的函数,但现在不会。
I might just redefine the responses to use numbers instead of strings to make it easier and faster to complete the entries, but at the moment it is not working so there is no point in doing that until I can get it figured out. 我可能只是重新定义了使用数字而不是字符串的响应,以便更轻松,更快速地完成输入,但是目前无法正常工作,因此在我弄清楚它之前没有任何意义。
The error is in the run_time()
section, it is not calling the function as it should be, I might be missing something. 错误发生在
run_time()
部分中,它没有按应有的方式调用函数,我可能丢失了一些东西。 I want it to call the function when the proper response is given. 我希望它在给出正确响应后调用该函数。
I did try: 我确实尝试过:
k = (query())
f = function_list[k]
f()
This did not worth either, so I am stuck. 这也不值得,所以我被困住了。 I appreciate any help.
感谢您的帮助。
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
There are two problems with your query
function: 您的
query
功能有两个问题:
return
anything; return
任何东西; and lower
. lower
。 Instead, try: 相反,请尝试:
def query():
prompt = 'What would you like to calculate? Degrees, Trapezoids, or Circles?'
return input(prompt).lower()
Note the following: 请注意以下几点:
input
already returns a string, no need to call str()
; input
已经返回一个字符串,无需调用str()
; return
the value; return
值; and ()
are required to actually call lower
, otherwise you return the method itself. lower
括号()
,否则返回方法本身。 Also, in f_to_c
, "flaot" != "float"
. 同样,在
f_to_c
, "flaot" != "float"
。
Finally, you could improve your run_time
function, minimising the amount of code in the try
block: 最后,您可以改善
run_time
函数,最大程度地减少try
块中的代码量:
try:
f = function_list[query()]
except KeyError:
print ("You messed up!")
else:
f()
This prevents a KeyError
from f()
being accidentally silenced. 这样可以防止
KeyError
导致f()
意外静音。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.