嘿,我有兩個錯誤,當我輸入飲料,並輸入糖的數量我得到「builtins.TypeError:'str'對象不可調用」,當我輸入AlcoholicDrink和金額的糖和酒精我得到「builtins.TypeError:無法將'float'對象隱式轉換爲str」。我仍然學習編碼,所以我不擅長錯誤任何幫助表示讚賞!不知道如何解決builtin.TypeError的
class Drink:
def __init__(self, sugar):
self.sugar = sugar
def numberOfCalories(self):
return 3.87 * sugar
class AlcoholicDrink(Drink):
def __init__(self, alcohol, sugar):
super().__init__(sugar)
self.alcohol = alcohol
def numberOfCalories(self):
if self.alcohol > 0:
return self.alcohol * 7.0 + self.sugar
else:
super().numberOfCalories()
choice = input('What would you like to drink?(Drink or AlcoholicDrink) ')
if (choice == 'AlcoholicDrink'):
sugar = float(input('How much sugar is in your drink? '))
alcohol = float(input('How much alcohol is in your drink? '))
userDrink = AlcoholicDrink(sugar, alcohol)
elif (choice == 'Drink'):
sugar = float(input('how much sugar is in your drink? '))
userDrink = Drink(sugar)
print ('Your amount of calories are ' + (userDrink.numberOfCalories()))
@bradmonster - 對不起,我沒有注意到你提到的其他錯誤。我已經爲此添加了修復程序。 – TigerhawkT3