2016-10-27 2 views
0

대부분 내 코드가 작동합니다. 당신이 그것을 실행하고 입력에 실수를하지 않으면, 그것은 의도대로 작동합니다. 귀하의 의견이 잘못된 경우, 해당 메시지는 말한다 다시 입력하라는 메시지를 표시하지만 다음 입력 유효한 숫자, 그것은 오류 메시지와 함께 나누는 경우 :코드 관련 문제 (파이썬)

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

당신나요 정말 클래스에 개체 인스턴스의 전역 이름 하드 코드? – TigerhawkT3

+0

정확히 입력 한대로 오류를 재현 할 수 없습니다 – Mangohero1

+0

@ mangoHero1 - 입력이 좋은 경우'yourQuakeInput'은'return richter'를 실행합니다. 그렇지 않으면,'r.yourQuakeInput()'만'return'을하지 않고 수행하기 때문에'None'입니다. 하지만 전반적으로 OP는 수업 및 '자기'사용법을 검토해야합니다. – TigerhawkT3

답변

-1

그것은 당신의 anwser이 잘못된 경우 반환 문에서도 누락 같다 : 다음은 코드 자체입니다

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

나는 ppl이 corrent를 downoatng하고 anwsers를 받아들이는 방법을 좋아한다. /에스 – Marcin