2017-10-24 13 views
-1

タイトルに記載されているとおり、自分のコードを使用するときに私はTypeError: 'float' object is not callableを得ています。 コードは、必要な情報で正しく1回実行されますが、for fare in faresを使用して2回目の実行に失敗します。ループの1回の繰り返し後にTypeErrorを取得しますか?

エラーメッセージはここにある:

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 

なぜこれが初めてではなく、2番目ではないのか分かりません。 私は学生であり、より高度なPythonコマンドについて多くの知識を持っていないかもしれないことにも留意してください。

ありがとうございます。

+1

に変数の名前を変更します。すべての変数名はちょうど1つのものを参照する必要があります。 –

答えて

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)) 
関連する問題