python - “x”是此函数的无效关键字参数

标签 python python-2.7

我正在尝试编写一些东西来检查整数 (z) 是否在 x 和 y 的范围内,x 是最小值,y 是最大值。我通过检查 z 是否小于最小值或大于最大值来执行此操作。如果其中任何一个为真,则返回无效,否则确认。检查值的最大值、最小值和个数由 raw_input 确定。这是代码:

int(x = raw_input('x:\n')) #max number
int(y = raw_input('y:\n')) #min number
int(z = raw_input('z:\n')) #number to check.
if z < y:
    print 'invalid.'
elif z > x :
    print 'invalid.'

我可以很好地输入 x 值,但是当我尝试输入 y 值时,出现此错误:

Traceback (most recent call last):
  File "C:/Python27/random/bark", line 2, in <module>
    int(y = raw_input('y:\n')) #min number
TypeError: 'y' is an invalid keyword argument for this function

我不知道我做错了什么,我在别处问过也没有答案。

最佳答案

代替这个:

int(x = raw_input('x:\n')) #max number

试试这个:

x = int(raw_input('x:\n')) #max number

其他输入语句也类似。

内置 Python 函数 raw_input() “从输入中读取一行,将其转换为字符串”。为了将输入用作整数,您需要借助 int()string 转换为 int将“字符串或数字转换为普通整数”的函数。从你的代码看来你有基本的想法,但你的语法有点困惑。

关于python - “x”是此函数的无效关键字参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11479791/

相关文章:

python - Django DB优化过滤器点击ip和1小时差异

python - 为什么 Python 的 `from` 形式的 import 语句会绑定(bind)模块名称?

python - 在 Django 1.7 中删除应用程序(和关联的数据库表)

python - 如何使用 '+' 作为输入

python-2.7 - Python >> matplotlib : Get the number of lines on a current plot?

python - 减去 Pandas 数据框

python - sns.regplot 显示了一个没有意义的回归阴影区域

python - 从列表列表中的字符串中删除字符

python - ftplib 执行时出错

python:如何将if语句转换为枚举或使其成为pythonic