简体   繁体   English

可变提示计算器输入

[英]Variable Tip Calculator Inputs

I am trying to create a tip calculator like every other newbie and of course I ran in to a problem. 我正在尝试创建一个像其他新手一样的小费计算器,当然我遇到了问题。 Most of the code works fine, but I can't figure out how to take user input for the tip and incorporate it into the grand total. 大部分代码都可以正常工作,但是我无法弄清楚如何将用户输入的技巧付诸实践。

This page was a good resource and told me that I need to point python in the direction to interpret the input as math used in the solution. 该页面是一个很好的资源,并告诉我,我需要将python指向将输入解释为解决方案中使用的数学的方向。 I wasn't able to translate it in my head into my code though. 但是我无法将其转换为代码。

Tip Calculator Function 提示计算器功能

# Tip Calculator
import random
bill = input("How much was your bill? ")
x = float(bill)
tip10 = x * .10
tip15 = x * .15

tip20 = x * .20

tip10 = float(tip10)
tip15 = float(tip15)
tip20 = float(tip20)

total = x + tip10
total = x + tip15
total = x + tip20
print(f"If you would like to leave a 10%, the tip amount will be ${tip10}.")
print(f"If you would like to leave a 15%, the tip amount will be ${tip15}.")
print(f"If you would like to leave a 20%, the tip amount will be ${tip20}.")
input("How much tip would you like to leave? ")
print(f"Your total is ${total:.2f}.)

When I run this it is only giving me the tip20 result after asking how much tip to leave, which I finally figured out is because it is the last line of the totals. 当我运行此程序时,在询问要离开多少小费后,才给我tip20结果,我最后才知道这是因为它是总数的最后一行。

How can I incorporate the user input into the total for the last line of code? 如何将用户输入合并到最后一行代码的总数中?

Ask the user for input again, and then calculate total based on what they say. 再次要求用户输入内容,然后根据他们的意见计算total

tipAmount = input("How much tip would you like to leave? (10, 15, 20) ")
if tipAmount == "10":
    total = x + tip10
elif tipAmount == "15":
    total = x + tip15
elif tipAmount == "20":
    total = x + tip20
else:
    total = x
    print("You're not leaving a tip? You cheapskate.")
print(f"Your total is ${total:.2f}")

I'll leave the problem of making sure the user input is one of those three options as an exercise to the reader (if you get stuck, take a look at this answer ). 我将保留确保用户输入是这三个选项之一作为读者练习的问题(如果您遇到困难,请看一下此答案 )。

There's no real need to hard-code each tip. 确实没有必要对每个提示进行硬编码。

# Tip Calculator
import random
bill = input("How much was your bill? ")
x = float(bill)
example_tips = [10, 15, 20]

for tip in example_tips:
    print(f"If you would like to leave a {tip}%, the tip amount will be ${x*tip/100}}.")
choice = input("How much tip would you like to leave? ")
total = x*(1+float(choice)/100)
print(f"Your total is ${total:.2f}.)

Here, choice is intended to be a percentage as in example_tips . 在这里, choice是作为example_tips的百分比。

input("How much tip would you like to leave? ") doesn't output to a variable. input("How much tip would you like to leave? ")不会输出到变量。

Also, total = x + tip20 is the only statement that has effect on total . 另外, total = x + tip20是唯一对total有影响的语句。 What you'll want to do is to add the user input to the variable by changing this: input("How much tip would you like to leave? ") to this: total = x + float(input("How much tip would you like to leave? ")) 您想要做的是通过将以下内容添加到变量中来添加用户输入: input("How much tip would you like to leave? ")到此: total = x + float(input("How much tip would you like to leave? "))

Of course, this is if you want the tips you output to be a sort of suggestion for the user. 当然,这是如果您希望输出的提示是对用户的一种建议。 If you want for the user to use your values, you're better off using Green Cloak Guy's answer. 如果要让用户使用您的值,最好使用Green Cloak Guy的答案。

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

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