简体   繁体   中英

Can someone tell me what im doing wrong? it keeps saying meal_cost is not defined

def solve(meal_cost, tip_percent, tax_percent):
    # Write your code here
    meal_cost = float(input())
    tip_percent = int(input())
    tax_percent = int(input())
    
    tip = meal_cost*tip_percent/100
    tax = meal_cost*tax_percent/100
    total_cost = meal_cost + tip + tax
    
    print(round(total_cost))
    
solve(meal_cost, tip_percent, tax_percent)

When you call solve , you have not defined any of the parameters you pass. The solution is to define them:

def solve(meal_cost, tip_percent, tax_percent):
    # Write your code here
    meal_cost = float(input())
    tip_percent = int(input())
    tax_percent = int(input())
    
    tip = meal_cost*tip_percent/100
    tax = meal_cost*tax_percent/100
    total_cost = meal_cost + tip + tax
    
    print(round(total_cost))
    


meal_cost = 3.0
tip_percentage = 0.19
tax_percent = 0.1
solve(meal_cost, tip_percent, tax_percent)

Note that this will overwrite the values passed in because you then ask to input the actual values. You can do this instead:

def solve(meal_cost, tip_percent, tax_percent):
    tip = meal_cost*tip_percent/100
    tax = meal_cost*tax_percent/100
    total_cost = meal_cost + tip + tax
    
    print(round(total_cost))
    
meal_cost = float(input())
tip_percent = int(input())
tax_percent = int(input())
solve(meal_cost, tip_percent, tax_percent)

as pointed out by Charles in the comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM