I';我试图用python创建一个程序,它接受用户提供的输入,并告诉他们是否';你是否未成年
我是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(“你多大了?”) 如果年龄
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正确。