简体   繁体   English

为什么我的小费计算器只返回 0.00 美元?

[英]why is my tip calculator just returning $0.00?

Im taking a course for learning python and this is the assignment they gave us我参加了学习python的课程,这是他们给我们的任务

"""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! """在美国,习惯上在餐厅用餐后给服务员留下小费,通常相当于您用餐费用的 15% 或更多。不过不用担心,我们已经编写了小费计算器给你,在下面!

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. Dollar_to_float,它应该接受一个 str 作为输入(格式为 $##.##,其中每个 # 是十进制数字),删除前导 $,并将金额作为浮点数返回。 For instance, given $50.00 as input, it should return 50.0.例如,给定 $50.00 作为输入,它应该返回 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. percent_to_float,它应该接受一个 str 作为输入(格式为 ##%,其中每个 # 是十进制数字),删除尾随 %,并将百分比作为浮点数返回。 For instance, given 15% as input, it should return 0.15.例如,给定 15% 作为输入,它应该返回 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? $50.00 你想给多少百分比的小费? 15% Leave $7.50 my problem is no matter what numbers i input, i end up getting "Leave $0.00" every single time. 15% 留下 7.50 美元 我的问题是无论我输入什么数字,我最终每次都会得到“留下 0.00 美元”。 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!请帮忙!

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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