【问题标题】:Why does this terminate after line 1? [closed]为什么这会在第 1 行之后终止? [关闭]
【发布时间】:2012-10-08 19:00:24
【问题描述】:

我正在尝试学习 Coursera 编程入门课程,并且基本上只是重新输入讲座中给出的示例以确保我理解内容。

通常,这很有效,但现在我遇到了一些代码,由于某种原因在第 1 行之后终止,我不希望它这样做。

def convert_to_celsius(fahrenheit):
    '''(number) --> number
Return the number of Celsius degrees equivalent to fahrenheit degrees.

>>>convert_to_celsius(32)
0
>>>convert_to_celsius(212)
100
'''
    return (fahrenheit - 32) * 5 / 9

这是哪里坏了?如何修复它以使其正常运行?

【问题讨论】:

  • 第 1 行在哪里?你想让这段代码做什么?
  • 你有没有调用这个函数?
  • 我认为它会被 doctests 调用......就是这样......
  • 请注意,由于 python 2.x 处理除法的方式,此代码无法在 python 2.x 上正常工作。

标签: python python-3.x


【解决方案1】:

我有点不确定你的问题是什么,但我猜你正在定义一个函数,但从不调用它。为了调用一个函数,你使用它的名字,并提供参数。例如:

#This next block defines the function
def convert_to_celsius(fahrenheit):
    return (fahrenheit - 32) * 5 / 9

#call/use the function
result = convert_to_celsius(100)
#print the results
print(result)

【讨论】:

    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2015-02-12
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多