2016-12-13 10 views
-1
print "Welcome to my converter!" 
#inches to centimeters 
def in_inches(n): 
    return(n * 2.54) 


#pounds to kilograms 
def in_pounds(n): 
    return(n * 0.453592) 

while True: 
    print "Which conversion?" 
    print "1 is for in to cm" 
    print "2 is for lbs to kg" 

    choice = raw_input("?") 

    if choice == "1": 
     n = raw_input("How many inches?") 
     res = in_inches(n) 
     print "In %s in we have %s cm." % (n, res) 
    elif choice == "2": 
     n = raw_input("How many pounds?") 
     res = in_pounds(n) 
     print str(n) + " is " + str(res) + " Kilograms" 
    else: 
     print "Invalid choice" 

私はこのコードを持っていますが、それをコンソールに導入しても機能しません。コンソールには次のようなメッセージが表示されます:私はこの簡単なコンバータプログラムで助けが必要です

Traceback (most recent call last): 
File "converter.py", line 20, in <module> 
res = in_inches(n) 
File "converter.py", line 4, in in_inches 
return(n * 2.54) 
TypeError: can't multiply sequence by non-int of type 'float' 

これはどういう意味ですか、どうすれば修正できますか? ありがとうございます。 `

+0

文字列に2.54を掛けることはできません。 'in_inches'で' float(n)* 2.54'を使うことができます。 – MSeifert

答えて

0

nあなたはこのように整数に変換する必要があり、文字列型である:

n = int(raw_input("How many inches?")) 

そして、

n = int(raw_input("How many pounds?")) 

raw_input()あなたがint()を使用する必要がある理由です、文字列を返しますnintに変換してください。

関連する問題