Task
Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.
Total meal Cost = meal cost + tip + tax )
def solve(meal_cost,tip_percent,tax_percent):
if __name__ == '__main__':
meal_cost = float(input())
tip_percent = int(input())
tax_percent = int(input())
tc = float(meal_cost + (tip_percent * meal_cost * 0.01) + (tax_percent * 0.01 * meal_cost))
print(round(tc))
solve(12,20,8)
The expected output isn't produced from the code. Can Anyone explain this error?. Error Message is "ValueError: could not convert string to float: '' ".
The Error comes from the input()
function.
Once you use the solve
function, the input()
functions are expecting values, but you probably type Enter to make solve
moving forward. Solve enterprete enter and raise an ValueError.
You should write your code like this:
def solve(meal_cost,tip_percent,tax_percent):
if __name__ == '__main__':
meal_cost = float(meal_cost)
tip_percent = int(tip_percent)
tax_percent = int(tax_percent)
tc = float(meal_cost + (tip_percent * meal_cost * 0.01) + (tax_percent * 0.01 * meal_cost))
print(round(tc))
solve(12,20,8)
15
However if you really want your function to be interactive using the input()
function, you could write it that way:
def solve():
if __name__ == '__main__':
meal_cost = float(input("enter meal_cost value : "))
tip_percent = int(input("enter tip_percent value : "))
tax_percent = int(input("enter tax_percent value : "))
tc = float(meal_cost + (tip_percent * meal_cost * 0.01) + (tax_percent * 0.01 * meal_cost))
print(round(tc))
solve()
By the way, no need to write if __name__ == '__main__':
inside your function ;-)