Python 在循环的一次迭代后获得TypeError?
正如标题中提到的,我在使用我的代码时遇到了一个Python 在循环的一次迭代后获得TypeError?,python,python-3.x,floating-point,typeerror,Python,Python 3.x,Floating Point,Typeerror,正如标题中提到的,我在使用我的代码时遇到了一个TypeError:“float”对象不可调用。 代码使用我想要的信息正确运行了一次,但使用票价中的票价第二次运行失败 错误信息如下: File "/Users/dty2004/Desktop/AS 91373.py", line 71, in <module> discounted_price(destination_fare) TypeError: 'float' object is not callable # Defin
TypeError:“float”对象不可调用。
代码使用我想要的信息正确运行了一次,但使用票价中的票价第二次运行失败
错误信息如下:
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))
我不确定为什么第一次这么做有效,第二次却不行。
还要记住,我是一名学生,因此可能不太了解更高级的python命令
提前感谢。您正在将折扣价格重新分配给以下变量:
# 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))
你在左右重复使用名字。每个变量名应该只引用一件事。
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))