Error – input expected at most 1 argument, got 3

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x