【问题标题】:This Python code for a Custom Calculator was working now I broke it and I can't figure out where I went wrong自定义计算器的这个 Python 代码现在可以工作了,但我把它弄坏了,我不知道哪里出错了
【发布时间】: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


【解决方案1】:

你的query函数有两个问题:

  1. 它没有return 任何东西;和
  2. 它不调用lower

请尝试:

def query():
    prompt = 'What would you like to calculate? Degrees, Trapezoids, or Circles?'
    return input(prompt).lower()

注意以下几点:

  • input已经返回一个字符串,不需要调用str()
  • 需要明确return的值;和
  • 括号() 是实际调用lower 所必需的,否则返回方法本身。

另外,在f_to_c"flaot" != "float"

最后,您可以改进run_time 函数,最大限度地减少try 块中的代码量:

try:
    f = function_list[query()]
except KeyError:
    print ("You messed up!")
else:
    f()

这可以防止 KeyErrorf() 被意外静音。

【讨论】:

  • 我完全忘记了那部分,非常感谢。我什至没有在寻找正确的东西。我真的很感激帮助。现在它为什么不起作用是有道理的。
  • 您没有充分利用将程序拆分为功能 - 如果您分别测试了每个功能(例如,实际上直接 calling query),您会看到这些问题.
  • 我明白你的意思,我正在使用这个计算器来练习我正在尝试学习的一些更高级的东西。我重写了整本书 3 次,试图找出哪里出错了。起初我让我的字典直接调用函数并且无法弄清楚为什么,这是因为我在字典中的函数上留下了括号,因此调用了它们。由于陷入了其他问题,我又创建了一些问题,再次感谢您的帮助。
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多