Home >Backend Development >Python Tutorial >Why Does My Python `input()` Function Throw a `TypeError: input expected at most 1 arguments, got 3`?
Troubleshooting TypeError: Input Expected at Most 1 Argument
While attempting to construct a number-guessing game in Python, numerous users encounter an error upon soliciting input from the user. To be specific, the offending line of code:
answer = input("Is it", guess, "?")
triggers the following error:
TypeError: input expected at most 1 arguments, got 3
The error's origin lies in the fact that the input function accepts only a single argument, whereas this code attempts to pass it three.
Resolution
To rectify this issue, modify the code to coalesce the multiple arguments into a singular argument. This can be achieved via string formatting or concatenation, as exemplified below:
answer = input(f"Is it {guess} ?")
In this revised code, the string formatting is accomplished through the use of the f-string syntax. This enables the interpolation of the guess variable into the string within the input function.
Distinction from Print Function
This particular error is frequently attributed to the confusion between the input and print functions. Unlike the input function, the print function accepts multiple arguments and amalgamates them into a single string.
The above is the detailed content of Why Does My Python `input()` Function Throw a `TypeError: input expected at most 1 arguments, got 3`?. For more information, please follow other related articles on the PHP Chinese website!