+ 1

unit converter

hi,guys here is my code: print "Hello! This is a unit converter between km and miles." km = " " miles = " " response = input('Enter km or miles: ') if response is 'km': print "Please enter a number of kilometers that you'd like " \ "to convert into miles. Enter only a number!" km = float(raw_input("Kilometers: ")) km = miles * 1.609 print "{0} kilometers is {1} miles.".format(km, miles) else: print "Please enter a number of miles that you'd like " \ "to convert into kilometers. Enter only a number!" miles = float(raw_input("Miles: ")) miles = km * 0.621 print "{0} miles is {1} kilometers.".format(miles,km) on two lines: km = miles * 1.609 and miles = km * 0.621 I got an error: TypeError: can't multiply sequence by non-int of type 'float' Also can you give me some directions for writing better codes. Thank you very much!!!

17th May 2018, 5:55 PM
Majdak84
Majdak84 - avatar
4 Réponses
+ 3
Please save the code in the Code Playground so it is easily editable and people could debug it easier. EDIT: And please, oh, please convert to Python3 ;)
17th May 2018, 6:16 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
It looks like python 2 code. Sololearn uses python 3. print without () won't work. raw_input doesn't work (It should be input). km and miles are initial given the value "". Later in the code they are used as float. Better make the initial value 0. If you input the value in km, you must calculate miles. And with miles you must calculate km.
17th May 2018, 6:42 PM
Paul
Paul - avatar
0
ok, yeah I'm on python 2.7. Thanks for your suggestions..
17th May 2018, 7:11 PM
Majdak84
Majdak84 - avatar
0
@ Jan Markus, I run your code and it works perfectly. Can you please explain a little bit,thx!!!
17th May 2018, 11:06 PM
Majdak84
Majdak84 - avatar