Seren Seren - 1 month ago 9
Python Question

Issue in code regarding functions (Python)

For the most part, my code works. If you run it and don't make any mistake in input, it works as intended. If your input is wrong, it says the appropriate message and prompts you to input again, but if you then input a valid number, it breaks down with the error message:

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'


I don't understand at all what this means, or how to fix it. I think it has something to do with the yourQuakeInput function, but I'm not sure. Does anyone have an idea? Here's the code itself:

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()

Answer

It seems you are missing return statments if your anwser are incorrect:

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()