Can I use a variable inside of an input statement? [duplicate]

不问归期 提交于 2021-02-05 05:01:01

问题


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

My goal is essentially a commission calculator. I am supposed to get the amount of sales per day from the user. However, I would like the user to know what day the information they are entering is for. The error I get says "input expected at most one arguments, got 2". So is there a way to use x in my input statement?


回答1:


You can use string formatting to insert the value of x in the string:

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

The current value of x will be inserted in the placeholder {}.



来源:https://stackoverflow.com/questions/39801441/can-i-use-a-variable-inside-of-an-input-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!