+ 4

What's wrong with this code?

I'm not too sure what's wrong with this def initial(): initial_unit = str(input("Enter the unit you currently have")) final_unit = str(input("Enter the unit you want to get")) if initial_unit == "stones" and final_unit == "kg": print(from_stones_to_kg()) elif initial_unit == "pounds" and final_unit == "kg": from_pounds_to_kg(int) elif initial_unit == "kg" and final_unit == "stones": from_kg_to_stones(int) elif initial_unit == "kg" and final_unit == "pounds": from_kg_to_pounds(int) else: return False initial() def from_stones_to_kg(stones): if stones != 0: newweight = stones * 6.35029 return newweight else: return 0 def from_pounds_to_kg(pounds): if pounds != 0: newweight = pounds * 0.453592 return newweight else: return 0 def from_kg_to_stones(kg): if kg != 0: newweight = kg / 6.35029 return newweight else: return 0 def from_kg_to_pounds(kg): if kg != 0: newweight = kg / 0.453592 return newweight else: return 0 kg = int(input("Enter the number of kg")) pounds = int(input("Enter the number of pounds")) stones = int(input("Enter the number of stones")) Thanks

8th Oct 2018, 12:00 PM
Irene
Irene - avatar
2 ответов
+ 4
You either have to define from_stones_to_kg() etc. before initial() or call initial() after you defined these functions. Otherwise you can't call the functions from initial(), because initial() doesn't "know" what these functions are. In lines 2 and 3 you convert the input to a string. That's not necessary as any user input will be a string by default. In line 5, you call from_stones_to_kg() without a parameter, but the function definition says that from_stones_to_kg() needs a parameter "kg". In lines 7, 9 and 11 you call functions with a parameter "int". int is a datatype. You need to call the function with a specific value instead. Maybe you want to do something like from_kg_to_stones(int(input('Enter the initial value'))). Also, you call the functions but you don't print their return value. So there will be no output.
8th Oct 2018, 12:27 PM
Anna
Anna - avatar
+ 2
This is the correct code with why I correcred everything https://code.sololearn.com/c0gTWpxyWQLT/?ref=app
8th Oct 2018, 12:29 PM
Daniele Bonomi
Daniele Bonomi - avatar