majk
majk

Reputation: 1

why is my tip calculator just returning $0.00?

Im taking a course for learning python and this is the assignment they gave us

"""In the United States, it’s customary to leave a tip for your server after dining in a restaurant, typically an amount equal to 15% or more of your meal’s cost. Not to worry, though, we’ve written a tip calculator for you, below!

def main():

dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
tip = dollars * percent
print(f"Leave ${tip:.2f}")
def dollars_to_float(d):
# TODO
def percent_to_float(p):
# TODO
main()

Well, we’ve written most of a tip calculator for you. Unfortunately, we didn’t have time to implement two functions:

dollars_to_float, which should accept a str as input (formatted as $##.##, wherein each # is a decimal digit), remove the leading $, and return the amount as a float. For instance, given $50.00 as input, it should return 50.0. percent_to_float, which should accept a str as input (formatted as ##%, wherein each # is a decimal digit), remove the trailing %, and return the percentage as a float. For instance, given 15% as input, it should return 0.15. Assume that the user will input values in the expected formats."""

What you should end up getting is How much was the meal? $50.00 What percentage would you like to tip? 15% Leave $7.50 my problem is no matter what numbers i input, i end up getting "Leave $0.00" every single time. this is what i have written

def main():
dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
tip = dollars * percent
print(f"Leave ${tip:.2f}")

def dollars_to_float(d):
str.lstrip(d)
return float(d)
def percent_to_float(p):
str.rstrip(p)
return float(p)
main()

please help!

Upvotes: -3

Views: 2133

Answers (2)

Abdullah
Abdullah

Reputation: 1

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")


def dollars_to_float(d):
    d = d.replace('$','')
    return float(d)


def percent_to_float(p):
    p = p.replace('%','')
    p = float(p) / 100
    return p


main()

Upvotes: 0

Muhammad Hamza
Muhammad Hamza

Reputation: 11

def main():
   dollars = dollars_to_float(input("How much was the meal? "))
   percent = percent_to_float(input("What percentage would you like to tip? "))
   tip = dollars * percent/100
   print(f"Leave $" , str(tip) )

def dollars_to_float(d):
   str.lstrip(d)
   return float(d)
def percent_to_float(p):
   str.rstrip(p)
   return float(p)
main()

Upvotes: 1

Related Questions