正如标题中所提到的,我在使用代码时获得TypeError: 'float' object is not callable
。
代码使用我想要的信息正确运行一次但无法使用for fare in fares
第二次运行。
错误信息在这里:
File "/Users/dty2004/Desktop/AS 91373.py", line 71, in <module>
discounted_price(destination_fare)
TypeError: 'float' object is not callable
代码在这里:
# Define the lists required for storing discount and discount_price
fare_auck = ["Auckland"]
fare_well = ["Wellington"]
fare_roto = ["Rotorua"]
# Make the lists into a 2D list
fares = [fare_auck, fare_well, fare_roto]
# Define the menu() function, which gives us the basic interface for
launching functions
def menu(destination, discounted_price, discount, saver_type,
original_price):
print("***** Waikato Air *****")
print("These saver fares for tomorrow only!")
print("Destination: {}".format(destination))
print("Discounted Price: ${:.2f}".format(discounted_price))
print("Percentage Discount: {:.2f}%".format(discount))
print("This fare is a: {}".format(saver_type))
print("Original Price: ${:.2f}".format(original_price))
print("") # Added an empty string to format the function to look nice
consecutively
# Define the function for destination determination
def destination_determiner():
if fares[0][0] == "Auckland":
travel_location = "Auckland"
fares[0].insert(0, "placeholder")
return travel_location
elif fares[1][0] == "Wellington":
travel_location = "Wellington"
fares[1].insert(0, "placeholder")
return travel_location
elif fares[2][0] == "Rotorua":
travel_location = "Rotorua"
fares[2].insert(0, "placeholder")
return travel_location
# Define the function for determining the type of saver fare this is
def saver_type(discount):
if discount < 20 and discount > -1:
saver_type = "Quick Saver"
return saver_type
elif discount >= 20 and discount <= 50:
saver_type = "Smart Saver"
return saver_type
elif discount > 50 and discount < 101:
saver_type = "Super Saver"
return saver_type
else:
print("Sorry that input is invalid")
# Define the function for determining the original_price
def original_price_calc(discount, discounted_price):
original_price = discounted_price * (discount / 100 + 1)
return original_price
# Define the function for getting discounted_price from user input
def discounted_price(destination):
discounted_price = float(input("Please input the discounted price to
{}".format(destination_fare)))
fare.append(discounted_price)
# Define the same function for getting discount from user input
def discount(destination):
discount = float(input("Please input the discount in percentage to
{}".format(destination_fare)))
fare.append(discount)
# Run the entire code, formatted into a print statement
for fare in fares:
destination_fare = destination_determiner()
discounted_price(destination_fare)
discount(destination_fare)
discounted_price = fare[2]
discount = fare[3]
menu(destination_fare, discounted_price, discount, saver_type(discount),
original_price_calc(discount, discounted_price))
在100和30的示例中使用接受值运行一次:
Please input the discounted price to Auckland100
Please input the discount in percentage to Auckland30
***** Waikato Air *****
These saver fares for tomorrow only!
Destination: Auckland
Discounted Price: $100.00
Percentage Discount: 30.00%
This fare is a: Smart Saver
Original Price: $130.00
我不确定为什么这是第一次工作而不是第二次工作。 另外请记住,我是一名学生,因此可能对更高级的python命令知之甚少。
提前致谢。
答案 0 :(得分:1)
您在此处将discounted_price重新分配给变量:
# Run the entire code, formatted into a print statement
for fare in fares:
destination_fare = destination_determiner()
discounted_price(destination_fare) # still a function
discount(destination_fare)
discounted_price = fare[2] # now a float!!!!
discount = fare[3] # same with this (overrides discount from earlier)
menu(destination_fare, discounted_price, discount, saver_type(discount),
original_price_calc(discount, discounted_price))
将变量重命名为其他内容
for fare in fares:
destination_fare = destination_determiner()
discounted_price(destination_fare)
discount(destination_fare)
discounted_price_value = fare[2]
discount_value = fare[3]
menu(destination_fare, discounted_price_value, discount_value,
saver_type(discount_value),
original_price_calc(discount_value, discounted_price_value))