有关函数的代码中的问题(Python)

繁荣

在大多数情况下,我的代码有效。如果您运行它并且没有在输入中犯任何错误,它将按预期工作。如果您输入的错误,则会显示相应的消息并提示您再次输入,但是如果您输入一个有效的数字,则会细分并显示错误消息:

Traceback (most recent call last):
  File "C:\Users\Seren\Documents\~School~\MSON Intro Computer Programming\Earthquake.py", line 92, in <module>
    print(r.yourQuakeOutput())
  File "C:\Users\Seren\Documents\~School~\MSON Intro Computer Programming\Earthquake.py", line 49, in yourQuakeOutput
    joules = r.calcJoules()
  File "C:\Users\Seren\Documents\~School~\MSON Intro Computer Programming\Earthquake.py", line 18, in calcJoules
    return 10 ** ((1.5 * self.r) + 4.8)
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

我完全不知道这是什么意思,或者如何解决。我认为这与yourQuakeInput函数有关,但我不确定。有人有主意吗?这是代码本身:

 class Earthquakes:

    def __init__(self, richter):
        self.r = richter

    def calcJoules(self):
        return 10 ** ((1.5 * self.r) + 4.8)

    def calcTNT(self):
        joules = r.calcJoules()
        return joules/(4.184 * 10 ** 9)

    def genericQuake(self):
        joules = r.calcJoules()
        tnt = r.calcTNT()
        return "An earthquake with a %s on the Richter scale is equivalent to %s joules and %s tons of exploded TNT." % (self.r, joules, tnt)

    def specQuake(self, quake):
        joules = r.calcJoules()
        tnt = r.calcTNT()
        return "The %s earthquake, a %s on the Richter scale, was equivalent to %s joules and %s tons of exploded TNT." % (quake, self.r, joules, tnt)

    def yourQuakeInput(self):
        richter = float(input("Your earthquake measures what on the Richter scale?"))
        if richter < 0:
            print("The Richter scale only goes from 0-10. Try again.")
            r.yourQuakeInput()
        elif richter > 10:
            print("The Richter scale only goes from 0-10. Try again.")
            r.yourQuakeInput()
        elif richter >= 0 and richter <= 10:
            return richter
        else:
            print("Invalid input. Try again.")
            r.yourQuakeInput()

    def yourQuakeOutput(self):
        joules = r.calcJoules()
        tnt = r.calcTNT()
        return "Your earthquake, a %s on the Richter scale, is equivalent to %s joules and %s tons of exploded TNT." % (self.r, joules, tnt)

    def __str__(self):
        return "richter=" + str(self.r)

def tryAgain():
    answer = input("Would you like to try again?").lower()
        if answer == "yes":
            pass
        elif answer == "no":
            quit()
        else: 
            print("Invalid input. Try again.")
            tryAgain()

r = Earthquakes (1.0)
print(r.genericQuake())
print("")

r = Earthquakes (5.0)
print(r.genericQuake())
print("")

r = Earthquakes (9.1)
print(r.specQuake("2004 Indonesia"))
print("")

r = Earthquakes (9.2)
print(r.specQuake("1964 Alaska"))
print("")

r = Earthquakes (9.5)
print(r.specQuake("1960 Chile"))
print("")

r = Earthquakes (5.8)
print(r.specQuake("2011 Virginia"))
print("")

while True:
    r = Earthquakes (r.yourQuakeInput())
    print(r.yourQuakeOutput())
    print("")

    tryAgain()
马辛

如果您的答案不正确,似乎您丢失了返回陈述:

if richter < 0:
    print("The Richter scale only goes from 0-10. Try again.")
    return r.yourQuakeInput()
elif richter > 10:
    print("The Richter scale only goes from 0-10. Try again.")
    return r.yourQuakeInput()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有关Python中的Lambda函数的问题

来自分类Dev

有关Raku中素数计算代码的问题

来自分类Dev

有关函数中多个参数的设计问题

来自分类Dev

Selenium,有关代码重构的问题

来自分类Dev

有关构造函数的问题

来自分类Dev

有关Python中@property的详细信息的问题

来自分类Dev

我在与元组有关的python中遇到问题

来自分类Dev

何时使用trait Instance()构造函数,以及有关traitsUI教程代码的问题

来自分类Dev

有关字符编码的Python问题

来自分类Dev

有关python ctypes模块的问题

来自分类Dev

Python ODEINT与args有关的问题

来自分类Dev

有关JavaScript中的cloneNode的问题

来自分类Dev

与访问中的关系有关的问题

来自分类Dev

有关R中优化的问题

来自分类Dev

有关SQL中通配符的问题

来自分类Dev

有关Java中别名的问题

来自分类Dev

我的代码中与while循环和数组有关的问题

来自分类Dev

这里的算法怎么了,代码与C中的递归问题有关?

来自分类Dev

有关cout和cin的C ++简单代码问题

来自分类Dev

有关代码放置的可变问题

来自分类Dev

有关数据类型的BeautifulSoup代码问题

来自分类Dev

有关通过迭代更改范围函数中的参数的问题

来自分类Dev

有关使用重写的函数的证明:“目标中的竖线”问题

来自分类Dev

有关SQL和AVG函数中的相关子查询的问题

来自分类Dev

有关构造函数的代码分析警告

来自分类Dev

有关简单C ++函数的逻辑的问题(is_palindrome)

来自分类Dev

有关pygame image.load函数的问题

来自分类Dev

有关修复错误的C函数的实践问题

来自分类Dev

与“在非对象上调用成员函数”有关的问题