2017-02-19 14 views
0

私のコードは華氏を摂氏に変換しています。これだよ。TryとExceptブロックを使用しますが、Exceptパートには定義されていませんPython

def again(): 
    calc = raw_input("Press y to convert again. Press n to quit. ").lower() 
    if calc == "y": 
     main() 
    elif calc == "n": 
     quit() 
    else: 
     again() 

def main(): 
    fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? ") 
    try: 
     celsius = (fahrenheit - 32) *5/float(9) 
     print('%0.2F degree Celsius is equal to about %0.1f degree Fahrenheit' %(celsius,fahrenheit)) 
     again() 
    except ValueError: 
     print("That is not a number") 
     check() 

実行する場合は、変換する数値の代わりに文字を入力します。 Exceptビットは実行されませんが、文字は定義されていません。 fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? ")を行った場合、引用符で囲まれた数字を引用符なしの通常の数字に変換するにはどうすればよいですか?私はValue Errorを選んだからだと思いますが、私は考えていません。どんな助けもありがとう!

+0

エラーを表示してください。 – cco

答えて

1

あなたのコードは、この例外が発生しますが、fahrenheit - 32を実行し、文字列から整数を引くしようとします。

TypeError: unsupported operand type(s) for -: 'str' and 'int' 

ValueErrorではないため、except節ではキャッチされません。

ValueErrorをキャッチする場合は、ブロックtry:fahrenheit = int(fahrenheit)を入れる必要があります。

0

3.xではなく、Python 2.xを使用しています。 Python 2.xでは、ではなく、inputを使用する必要があります。これに

fahrenheit = input(
    "What do you want to convert from Fahrenheit to Celsius? ") 

fahrenheit = raw_input(
    "What do you want to convert from Fahrenheit to Celsius? ") 

のpython 2.7に失敗input()を示すファイル名を指定して実行:

C:\Python27\python.exe test.py 
What do you want to convert from Fahrenheit to Celsius? d 
Traceback (most recent call last): 
    ... 
    "What do you want to convert from Fahrenheit to Celsius? ") 
    File "<string>", line 1, in <module> 
NameError: name 'd' is not defined 

のpython 3.5で作業input()を示すファイル名を指定して実行:

だから、これを変更します
"C:\Program Files (x86)\Python35-32\python.exe" test.py 
What do you want to convert from Fahrenheit to Celsius? d 
Traceback (most recent call last): 
    ... 
    File "C:/Users/stephen/Documents/src/testcode/test.py", line 31, in main 
    celsius = (fahrenheit - 32) * 5/float(9) 
TypeError: unsupported operand type(s) for -: 'str' and 'int' 

のpython 2.7で作業raw_input()を示すファイル名を指定して実行:

C:\Python27\python.exe test.py awk_data.txt 
What do you want to convert from Fahrenheit to Celsius? d 
Traceback (most recent call last): 
... 
    File "C:/Users/stephen/Documents/src/testcode/test.py", line 31, in main 
    celsius = (fahrenheit - 32) * 5/float(9) 
TypeError: unsupported operand type(s) for -: 'str' and 'int' 
0

次のように試してみるの整数にユーザからの入力を変換します

fahrenheit = int(fahrenheit) 

あなたはPythonの3.xを使用している場合、これはあなたの最終的なコード

def again(): 
calc = input("Press y to convert again. Press n to quit. ").lower() 
if calc == "y": 
    main() 
elif calc == "n": 
    quit() 
else: 
    again() 

def main(): 
fahrenheit = input("What do you want to convert from Fahrenheit to Celsius? ") 
try: 
    fahrenheit = int(fahrenheit) 
    celsius = (fahrenheit - 32) *5/float(9) 
    print('%0.2F degree Celsius is equal to about %0.1f degree Fahrenheit' %(celsius,fahrenheit)) 
    again() 
except ValueError: 
    print("That is not a number") 
    #check() 
main() 

である必要があり、それは

を役に立てば幸い
+0

コードが生成されますが、文字が渡された場合はValueErrorをキャッチしません。 – cco

+0

ValueErrorを生成します。私はちょうどそれをチェックする。 – vacky

+0

はい、それは行いますが、 'try'の内部にはありませんので、捕まえられず、"それは数字ではありません "と表示されます。 – cco

関連する問題