Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 299
Dustin Burns Avatar asked Jan 18 '23 01:01

Dustin Burns


2 Answers

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

score = int(input('Please enter your score for test ' + str(y) + ': '))
like image 191
Óscar López Avatar answered Jan 29 '23 23:01

Óscar López


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))
like image 43
paxdiablo Avatar answered Jan 29 '23 23:01

paxdiablo