避免 python中的 typeerror: input expected at most 1 argument, got 3 错误-ag捕鱼王app官网

避免 python中的 typeerror: input expected at most 1 argument, got 3 错误

作者:迹忆客 最近更新:2023/07/06 浏览次数:

在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() 函数的参数,使它们成为单个参数并避免输入类型错误。

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2023/07/09 浏览次数:173 分类:python

我们将通过示例介绍python中何时出现 address already in use 错误以及如何解决。python 中的错误address already in use 本文将讲述运行使用端口的程序时发生的python堆栈错误。

发布时间: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错误 让我们尝试理解为什么会发生这个特定的错误。 让我们首先尝试复制这个问题。

发布时间:2023/07/09 浏览次数:2241 分类:python

在 python 中,attributeerror 是在未定义 __enter__ 函数的情况下通过 with 语句使用类的对象时导致的错误。

发布时间:2023/07/09 浏览次数:1063 分类:python

本篇文章将介绍如何修复 python 中的 attributeerror: '_io.textiowrapper' object has no attribute 'split'。在 _io.textiowrapper 上使用 split() 方法会返回 attributeerror

python 错误 error: invalid command bdist_wheel

发布时间:2023/07/09 浏览次数:847 分类:python

在 python 中构建 wheel 时,有时 setup.py 可能会退出并出现错误 invalid command 'bdist_wheel'。 本篇文章将讨论在 python 中解决此问题的可能ag捕鱼王app官网的解决方案。安装wheel包来修复python中 error:invalid command 'bdist_

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图