为什么我的提示计算器刚刚返回$ 0.00?

发布于 2025-02-02 02:16:00 字数 1059 浏览 4 评论 0原文

我正在学习学习python的课程,这是他们给我们的任务

“”“在美国,在餐厅用餐后为您的服务器留下小费,通常等于您的餐点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()

已经为您写了大部分提示计算器。

我们 应接受str作为输入(格式为$ ##。##,其中每个#都是十进制数字),删除领先的$,并将其返回为float,例如$ 50.00。 。 percon_to_float应该接受str作为输入(格式为##%,其中每个#都是十进制数字),删除尾随%,然后将百分比返回为浮点。例如,给定15%作为输入,应返回0.15。 假设用户将以预期格式输入值。”““

您最终应该得到的是 这顿饭是多少? $ 50.00 您想提示什么百分比? 15% 留下$ 7.50 我的问题是无论我输入什么数字,我最终每次都会获得“ $ 0.00”。这就是我写的,

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

请帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2025-02-09 02:16:00
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 
quot; , 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()
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 
quot; , 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()
爱的十字路口 2025-02-09 02:16:00
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()
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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文