Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在这个基本python代码中,我做错了什么?_Python_Python 3.x - Fatal编程技术网

在这个基本python代码中,我做错了什么?

在这个基本python代码中,我做错了什么?,python,python-3.x,Python,Python 3.x,我正在测试if语句,我遇到了这个难题。我不明白我做错了什么,为什么它需要字符串而不是整数。这是我的密码 # If the entered age was over 21 print "have a drink" otherwise print the other one. age = input(int("what's your age?:\n\t")) if age >= 21 : print("have a drink") else: print("you're jus

我正在测试if语句,我遇到了这个难题。我不明白我做错了什么,为什么它需要字符串而不是整数。这是我的密码

# If the entered age was over 21 print "have a drink" otherwise print the other one.
age = input(int("what's your age?:\n\t"))
if age >= 21 :
    print("have a drink")
else:
    print("you're just a lad!")

当您有嵌套函数调用时,它们是由内而外执行的,每个结果都作为包含它的下一个结果的参数。因此:

age = input(int("what's your age?:\n\t"))
相当于:

temp = int("what's your age?:\n\t")
age = input(temp)
以这种方式编写时,您可以看到给定给
input()
的参数是
int()
返回的整数。此外,您给
int()
的参数不能有意义地转换为整数

正确的语法是:

age = int(input("what's your age?:\n\t"))

这将首先调用
input()
,然后将响应转换为整数。

int(input())
,而不是
input(int())
。要扩展@internet\u user所说的内容,请尝试将以下字符串:“您的年龄是多少?”:\n\t”转换为整数。您的顺序应该是首先获取输入,然后按照上面的建议将其设置为整数。谢谢!这清除了我的很多代码