【问题标题】:Can I use a variable inside of an input statement? [duplicate]我可以在输入语句中使用变量吗? [复制]
【发布时间】:2016-09-30 23:29:46
【问题描述】:
def main():
    total = 0.0
    totalcom = 0.0
    name = input("Please enter your name: ")
    for x in range(1, 8):
        sales = float(input("Please enter your sales from day", x))

        total += sales
        commission = sales * .1
        totalcom += commission

    print("Your total sales is: ", total)
    print("Your commission is: ", totalcom)


main()

我的目标本质上是一个佣金计算器。我应该从用户那里获得每天的销售额。但是,我希望用户知道他们输入的信息是针对哪一天的。我得到的错误是“最多输入一个参数,得到 2 个”。那么有没有办法在我的输入语句中使用 x 呢?

【问题讨论】:

    标签: python input arguments


    【解决方案1】:

    您可以使用string formatting 在字符串中插入x 的值:

    sales = float(input("Please enter your sales from day {}".format(x)))
    

    x 的当前值将插入占位符{}

    【讨论】:

    • 谢谢!这正是我想要的。没见过以前用过的。我喜欢它
    • 哇,这里是严酷的社区吧?您的回答很完美,但由于我在这里的代表,我无法投票。为什么投反对票?
    猜你喜欢
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 2020-11-24
    • 1970-01-01
    • 2013-07-10
    相关资源
    最近更新 更多