避免 Python中的 TypeError: Input Expected at Most 1 Argument, Got 3 错误
在Python编程中,我们有两个内置方法来获取用户的输入:input(prompt)
和 raw_input(prompt)
。 第二种方法 raw_input(prompt)
在Python 2.X等老版本中使用,input(prompt)
仍在使用。
本文将重点介绍 input(prompt)
方法。
在 Python 中获取用户输入
在Python中,我们将使用内置的 input()
函数来获取用户的输入并在程序中相应地使用它。
Python 中 input()
函数的使用:
name = input("Hey! What is your name? ")
print("Nice to meet you ", name)
输出:
Hey! What is your name? Zeeshan Afridi
Nice to meet you Zeeshan Afridi
在上面的程序中,我们使用了 input(prompt)
函数来询问用户的姓名。 当用户输入他们的名字时,它将存储在 name 变量中,您稍后可以在程序中任何需要的地方使用它。
Python 中避免 TypeError: input Expected atmost 1 argument, got 3 Error
在计算机编程中,TypeError 是一种语法错误,意味着开发人员由于输入语法无效或向函数传递额外参数而在编写程序时犯了一些错误。 常见错误之一是 TypeError: input Expected atmost 1 argument, got 3 Error。
让我们通过一个例子来理解它:
name = input ("Please enter your name: ")
age = input ("Please enter your age: ")
print("Your good name is ", name, " and your age is ", age)
goal = input("What is your life goal Mr. ", name, " ?")
print(goal)
print("Awesome!")
输出:
Please enter your name: Zeeshan Afridi
Please enter your age: 24
Your good name is Zeeshan Afridi and your age is 24
TypeError: input expected at most 1 argument, got 3
上面的程序中,前3行执行得很顺利,但是下一行代码 goal = input("What is your life goal Mr. ", name, " ?")
却抛出了错误 TypeError: input Expected atmost 1 argument, got 3 Error。
这是因为 Python input()
函数只需要一个参数,但我们已将三个参数传递给 input()
函数。 第一个参数是字符串 What is your life goal Mr. ,第二个参数是变量名,最后一个参数又是一个字符串 ? 。
我们用逗号 , 分隔这些参数,这表明每个参数都是一个单独的参数。 但是,如果您想按原样打印输入提示,则必须连接这些参数以使其成为单个参数以满足 input()
函数的期望。
让我们通过一个例子来理解它:
name = input ("Please enter your name: ")
age = input ("Please enter your age: ")
print("\nYour good name is ", name, " and your age is ", age)
goal = input("What is your life goal Mr. "+ name+ " ? ")
print("Awesome!")
输出:
Please enter your name: Zeeshan Afridi
Please enter your age: 24
Your good name is Zeeshan Afridi and your age is 24
What is your life goal Mr. Zeeshan Afridi ? To be a Python Developer
Awesome!
在上面的程序中,我们借助加号 +
连接了 input()
函数的参数,使它们成为单个参数并避免输入类型错误。
相关文章
Python 中错误 Address Already in Use
发布时间:2023/07/09 浏览次数:173 分类:Python
-
我们将通过示例介绍Python中何时出现 Address already in use 错误以及如何解决。Python 中的错误Address already in use 本文将讲述运行使用端口的程序时发生的Python堆栈错误。
Python 中错误 ValueError: Math Domain Error
发布时间:2023/07/09 浏览次数:607 分类:Python
-
在本篇文章中,我们的目标是探索解决 Python 中的 ValueError: math domain error 错误的不同方法。当编码方面数学(基础或高级)的使用存在固有缺陷时,Python 中通常会引发 ValueError: math domain error 错
Python 错误 Name xrange Is Not Defined
发布时间:2023/07/09 浏览次数:153 分类:Python
-
本篇文章将介绍如何解决 Python 中 name 'xrange' is not defined 的错误。解决Python中name 'xrange' is not defined错误 让我们尝试理解为什么会发生这个特定的错误。 让我们首先尝试复制这个问题。
Python 错误 TypeError: List Indices Must Be Integers, Not STR
发布时间:2023/07/09 浏览次数:954 分类:Python
-
在本篇文章中,我们的目标是探索如何避免 TypeError: list indices must be integers or slices, not str。TypeError主要发生在Python中,每当操作的数据类型出现问题时。
Python 中错误 AttributeError: __Enter__
发布时间:2023/07/09 浏览次数:2241 分类:Python
-
在 Python 中,AttributeError 是在未定义 __enter__ 函数的情况下通过 with 语句使用类的对象时导致的错误。
Python 错误 ModuleNotFoundError: No Module Named '_Ctypes'
发布时间:2023/07/09 浏览次数:686 分类:Python
-
本篇文章旨在了解如何解决 Python 中的 ModuleNotFoundError: No module named '_ctypes'。了解Python中 ModuleNotFoundError: No module named '_ctypes' 根本原因
Python 错误 AttributeError: '_io.TextIOWrapper' Object Has No Attribute 'Split'
发布时间:2023/07/09 浏览次数:1063 分类:Python
-
本篇文章将介绍如何修复 Python 中的 AttributeError: '_io.TextIOWrapper' object has no attribute 'split'。在 _io.TextIOWrapper 上使用 split() 方法会返回 AttributeError
Python 错误 AttributeError: _csv.reader Object Has No Attribute Next
发布时间:2023/07/09 浏览次数:286 分类:Python
-
本篇文章将介绍如何修复 Python 中的 AttributeError: '_csv.reader' object has no attribute 'next'。修复 Python 中的 AttributeError: '_csv.reader' object has no attribute 'next' 错误
Python 错误 Error: Invalid Command Bdist_wheel
发布时间:2023/07/09 浏览次数:847 分类:Python
-
在 Python 中构建 wheel 时,有时 setup.py 可能会退出并出现错误 invalid command 'bdist_wheel'。 本篇文章将讨论在 Python 中解决此问题的可能解决方案。安装wheel包来修复Python中 Error:invalid command 'bdist_