我可以在输入语句中使用变量吗?

Der*_*ick 3 python arguments input

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()
Run Code Online (Sandbox Code Playgroud)

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

Mos*_*oye 9

您可以使用字符串格式来插入字符串中的值x:

sales = float(input("Please enter your sales from day {}".format(x)))
Run Code Online (Sandbox Code Playgroud)

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