-3

The line

question = str(input("What is",randomNumber1,"+",randomNumber2,"x",randomNumber3,"?\n"))

in my code is giving me trouble.

This is the error I get:

question = str(input("What is",randomNumber1,"+",randomNumber2,"x",randomNumber3,"?\n"))
TypeError: input expected at most 1 arguments, got 7

If you could help it would be much appreciated as I don't know what I have done wrong.

timgeb
  • 76,762
  • 20
  • 123
  • 145

4 Answers4

1

You are using , in your parentheses for your strings. So Python thinks, these are parameters for your called function. You need to append your strings together (via + as already mentioned).

Furthermore, you should consider raw_input in Python2, because input is interpreted as Python code: look here

Community
  • 1
  • 1
nox
  • 252
  • 5
  • 18
0

Like the output already says.

question = str(input("What is"+randomNumber1+"+"+randomNumber2+"x"+randomNumber3+"?\n"))
Philipp Braun
  • 1,583
  • 2
  • 25
  • 41
  • In addition, use `raw_input(prompt)` instead of `str(input(prompt))` assuming OP is using Python2. – timgeb Jan 30 '16 at 11:50
0

Are you Python 3? If you are Python 2 then you should use raw_input() instead of input(). If you are using Python 3 then please try to use that tag (most will assume "Python" means Python 2).

input() and raw_input() both return strings (no need to force it), and they only take one argument, as the error message said. Also your comparison for the correct answer uses different types, you are comparing a string with an int.

Best to construct the question first as a string:

question = "What is %d + %d x %d? " % (randomNumber1,randomNumber2,randomNumber3)

users_answer = input(question)

answer = randomNumber1 + randomNumber2 * randomNumber3

# users_answer and answer are different types
if int(users_answer) == answer:
    print("\n[ Correct ]\n")
    playerScore = playerScore + 1
    print("Your score is",playerScore)
    questionNumber = questionNumber + 1

else:
    print("\n[ Incorrect ]\n")
    questionNumber = questionNumber + 1
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • 1
    The plain python tag covers both Python 2 and Python 3. All Python questions should use the plain python tag, but the version-specific tag should also be given, when appropriate. Of course, new Python coders may not realise when they need to mention the version. :) FWIW, in the [SO Python Chat room](http://chat.stackoverflow.com/rooms/6/python) the current policy is to assume Python 3 if no version is mentioned. – PM 2Ring Jan 30 '16 at 13:13
  • @PM2Ring: that's good to know, but judging from the replies (use of `input()`) to this particular question, others also make the assumption. – cdarke Jan 31 '16 at 07:24
  • 1
    Yes, I noticed that others were making that assumption, I just figured your answer was a reasonable place to put my comment. FWIW, on my Python 2.6.6 system the error message I get is `TypeError: [raw_]input expected at most 1 arguments, got 7`. Curiously, `input` / `raw_input` can be passed a tuple (or any other object) as the prompt argument, and its `__str__` method will be called to convert it to a string. – PM 2Ring Jan 31 '16 at 07:36
  • @PM2Ring: Yes it was a reasonable comment, thank-you. `input()` gives the same behaviour on Python 3. I always reckon that I learn more by answering questions than asking them!! – cdarke Jan 31 '16 at 07:40
  • Ah, ok. I just didn't see the **[raw_]** in the OP's error message, so I assumed that was due to the OP using Python 3 (or at least a more recent version of Python than what I have on this ancient machine). – PM 2Ring Jan 31 '16 at 07:43
  • 1
    @PM2Ring: the `print()` *function* can be a give-away as well, although that is not bullet-proof. I have seen at least one question where `input()` was incorrectly used on Python 2 (the OP had copied some 3 code). I would like at least the Python major version to be included in all Python questions. – cdarke Jan 31 '16 at 07:51
  • 1
    Definitely. If I had a dollar for every Unicode question that completely fails to give a clue as to what Python version is being used... – PM 2Ring Jan 31 '16 at 08:04
0

You are using , while calling the function input(). As a result, python interprets it to be 7 different arguments.

I guess, the following code will do what you require.

question = str(input("What is " + str(randomNumber1) + " + " + str(randomNumber2) + " x " + str(randomNumber3) + " ?\n"))

Do note, this will store the answer provided by the user as a string in the variable question.

If you require to accept the answer as an integer (number) use the following instead.

question = input("What is " + str(randomNumber1) + " + " + str(randomNumber2) + " x " + str(randomNumber3) + " ?\n")
Pratanu Mandal
  • 597
  • 8
  • 23