Home >Backend Development >Python Tutorial >Why Does My Python Guessing Game Throw a 'TypeError: input expected at most 1 arguments, got (more than 1)' Error?

Why Does My Python Guessing Game Throw a 'TypeError: input expected at most 1 arguments, got (more than 1)' Error?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-14 08:57:10286browse

Why Does My Python Guessing Game Throw a

Troubleshooting "TypeError: input expected at most 1 arguments, got (more than 1)

When attempting to create a Python guessing game in which the computer guesses a number chosen by the player, you may encounter the following error:

TypeError: input expected at most 1 arguments, got 3

This error arises from using the input function incorrectly. The input function can only accept a single argument, but you are trying to pass it multiple arguments.

To resolve this issue, you need to use string concatenation or formatting to combine your arguments into a single string that the input function can accept. Here are two ways to do this:

  1. String Concatenation:
guess = 5
answer = input("Is it " + str(guess) + "?")
  1. String Formatting:
guess = 5
answer = input(f"Is it {guess} ?")

In either case, the string concatenation or formatting will create a single string that contains both the text you want to display and the variable guess. The input function can then accept this single string as its argument.

Remember, the print function behaves differently from the input function. print can accept multiple arguments and will automatically concatenate them into a single string. However, input requires only a single argument, which must be a string.

The above is the detailed content of Why Does My Python Guessing Game Throw a 'TypeError: input expected at most 1 arguments, got (more than 1)' Error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn