Error - input expected at most 1 argument, got 3

浪尽此生 提交于 2019-11-26 23:38:36

问题


I've set up the following for loop to accept 5 test scores. I want the loop to prompt the user to enter 5 different scores. Now I could do this by writing the input "Please enter your next test score", but I'd rather have each inputted score prompt for its associated number.

So, for the first input, I'd like it to display "Please enter your score for test 1", and then for the second score, display "Please enter your score for test 2". When I try to run this loop, I get the following error:

Traceback (most recent call last):
  File "C:/Python32/Assignment 7.2", line 35, in <module>
    main()
  File "C:/Python32/Assignment 7.2", line 30, in main
    scores = input_scores()
  File "C:/Python32/Assignment 7.2", line 5, in input_scores
    score = int(input('Please enter your score for test', y,' : '))
TypeError: input expected at most 1 arguments, got 3

Here's the code

def input_scores():
    scores = []
    y = 1
    for num in range(5):
        score = int(input('Please enter your score for test', y, ': '))

        while score < 0 or score > 100:
            print('Error --- all test scores must be between 0 and 100 points')
            score = int(input('Please try again: '))
        scores.append(score)
        y += 1
        return scores

回答1:


A simple (and correct!) way to write what you want:

score = int(input('Please enter your score for test ' + str(y) + ': '))



回答2:


Because input does only want one argument and you are providing three, expecting it to magically join them together :-)

What you need to do is build your three-part string into that one argument, such as with:

input("Please enter your score for test %d: " % y)

This is how Python does sprintf-type string construction. By way of example,

"%d / %d = %d" % (42, 7, 42/7)

is a way to take those three expressions and turn them into the one string "42 / 7 = 6".

See here for a description of how this works. You can also use the more flexible method shown here, which could be used as follows:

input("Please enter your score for test {0}: ".format(y))


来源:https://stackoverflow.com/questions/9969844/error-input-expected-at-most-1-argument-got-3

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