简体   繁体   English

这个自定义计算器的Python代码现在可以正常工作了,我把它弄坏了,无法弄清楚哪里出了问题

[英]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功能有两个问题:

  1. It doesn't return anything; 它不return任何东西; and
  2. It doesn't call 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()
  • You need to explicitly return the value; 您需要显式return值; and
  • Parentheses () 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM