Python报错TypeError: input最多接受1个参数却传入3个,如何修复?
First, let's break down why you're hitting that error:
TypeError: input expected at most 1 arguments, got 3
The input() function only accepts a single string as its argument. When you use commas to split your prompt ("type number ",n+1," : value "), you're passing three separate values to input() instead of one combined string. That's exactly what's causing the error.
How to Fix It
You need to merge your prompt text and variable into a single string. Here are three straightforward ways to do this in Python:
1. Use F-Strings (Recommended for Python 3.6+)
F-strings are the cleanest and most readable option for formatting strings with variables. Just add an f before your quoted string, and wrap variables in curly braces {}:
n = int(input("type how many input : ")) if n != 0: small = int(input("type number 1 value : ")) for i in range(1, n): # Combine text and variable with an f-string temp = int(input(f"type number {i+1} : value ")) if temp < small: small = temp print(small)
2. Use str.format()
If you're working with an older Python version (pre-3.6), use the format() method to insert variables into your string:
n = int(input("type how many input : ")) if n != 0: small = int(input("type number 1 value : ")) for i in range(1, n): temp = int(input("type number {} : value ".format(i+1))) if temp < small: small = temp print(small)
3. String Concatenation with +
You can also glue strings together using the + operator, but you'll need to convert the integer i+1 to a string first with str():
n = int(input("type how many input : ")) if n != 0: small = int(input("type number 1 value : ")) for i in range(1, n): temp = int(input("type number " + str(i+1) + " : value ")) if temp < small: small = temp print(small)
Bonus Fix: Correcting the Prompt Number
I noticed your original code used n+1 in the prompt, which would show the same incorrect number every time (e.g., if n=3, it would always say "number 4"). Changing it to i+1 ensures each prompt displays the right sequential number (2, 3, ..., n) for each input.
Edge Case Note
Your code handles n=0 by skipping the input loop, but when n=0, print(small) will throw a NameError because small isn't defined. To fix this, you could initialize small to a default value and add a check before printing:
n = int(input("type how many input : ")) small = None if n != 0: small = int(input("type number 1 value : ")) for i in range(1, n): temp = int(input(f"type number {i+1} : value ")) if temp < small: small = temp if small is not None: print(small) else: print("No numbers were entered.")
内容的提问来源于stack exchange,提问作者The Temp Guy

