Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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
I';我试图用python创建一个程序,它接受用户提供的输入,并告诉他们是否';你是否未成年_Python_If Statement_Statements - Fatal编程技术网

I';我试图用python创建一个程序,它接受用户提供的输入,并告诉他们是否';你是否未成年

I';我试图用python创建一个程序,它接受用户提供的输入,并告诉他们是否';你是否未成年,python,if-statement,statements,Python,If Statement,Statements,我是python新手,这是我提出的,它不起作用 age = input("How old are you? ") if age < 18 print("You are under age.") else print("You are over age") age=input(“你多大了?”) 如果年龄

我是python新手,这是我提出的,它不起作用

age = input("How old are you? ")

if age < 18
    print("You are under age.")
else
    print("You are over age")
age=input(“你多大了?”)
如果年龄<18岁
打印(“您未成年。”)
其他的
打印(“您已超龄”)

谢谢。

必须使用
int
构造函数将
input
函数的结果转换为
int
,如
int(“123”)
,以便与数字
18
进行比较

if int(input("How old are you?")) < 18:
  print("You are under age")
else:
  print("You are over age")
if int(输入(“你多大了?”)<18:
打印(“您未成年”)
其他:
打印(“您已超龄”)

必须使用
int
构造函数将
input
函数的结果转换为
int
,如
int(“123”)
,以便与数字
18
进行比较

if int(input("How old are you?")) < 18:
  print("You are under age")
else:
  print("You are over age")
if int(输入(“你多大了?”)<18:
打印(“您未成年”)
其他:
打印(“您已超龄”)

输入的类型是什么?嗯,让我们打开REPL看看

$ ipython
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: age = input('how old are you?')
how old are you?10

In [2]: type(age)
Out[2]: str

In [5]: age == '10'
Out[5]: True

In [6]: age == 10
Out[6]: False
查看Python如何处理与
int
不同的类型
str


您还忘记了在您的
if statemt

之后的冒号
输入的类型是什么?嗯,让我们打开REPL看看

$ ipython
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: age = input('how old are you?')
how old are you?10

In [2]: type(age)
Out[2]: str

In [5]: age == '10'
Out[5]: True

In [6]: age == 10
Out[6]: False
查看Python如何处理与
int
不同的类型
str


如果statemet

input()
返回字符串,则在
之后也会忘记冒号
。将其转换为整数:
age=int(输入(“你多大了?”)
除的注释外,if
else
之后缺少冒号欢迎使用堆栈溢出。请拿着这本书读一读。这段代码将产生一条有用的错误消息,您在这里没有包括这条消息。请始终阅读并共享错误消息。他们在那里不是为了好玩;它们实际上很有用。
input()
返回字符串。将其转换为整数:
age=int(输入(“你多大了?”)
除的注释外,if
else
之后缺少冒号欢迎使用堆栈溢出。请拿着这本书读一读。这段代码将产生一条有用的错误消息,您在这里没有包括这条消息。请始终阅读并共享错误消息。他们在那里不是为了好玩;他们真的很有帮助。非常感谢!所以我需要在输入之前加一个int,因为我想要的答案是一个数字,对吗?我还忘了:@LucasBarbieri正确。非常感谢!所以我需要在输入之前加一个int,因为我想要的答案是一个数字,对吗?我还忘了:@LucasBarbieri正确。