python - 我的代码返回了错误的 if 语句
问题描述
我正在尝试编写一个简单的代码,它将为 18 岁以上的学生打开一扇门,但无论我输入什么数字,它总是在 if 语句中返回错误的选项。请看下面。
Def checkStudentAge:
Input("what is your name student?: ")
Age = 0
If int(age) < 18
Print("this door only opens for students that are 18 are above")
elif int(age) > 18
Print(" you are welcome, please push the door and step in")
else int(age) ==18
Print("congrats you can step in)
CheckStudentage()
每当我调用该函数并输入一个高于 18 的 int 时,它仍然说“这扇门只为 18 岁及以上的学生打开”。我是 python 新手,这是我的第一语言,不胜感激
我做了年龄= 0,因为我想为年龄分配一个值,以防没有输入
解决方案
def checkStudentAge():
age = int(input("age: "))
if age < 18:
print('This door opens for students who are 18 or above')
else:
print('Welcome! Please push the door and step in')
""" The if-else can also be combined into one line like so:
print('This door opens for students who are 18 or above') if age < 18 else print('Your are welcome, please push the door and step in')
"""
checkStudentAge()
elif
andelse
可以合二为一,else
因为年龄是 [小于 18] 或 [大于或等于]。
推荐阅读
- tensorflow - 将多个不同序列长度的输入数据传递给 Keras
- r - 需要使用另一个文件上的列信息对主数据框进行子集化
- flutter - 浮动动作按钮中的颤动位置文本?
- kubernetes - 如何获取除主节点外的所有状态为“就绪”的节点?
- java - 尝试在我的手机上对应用程序进行负载测试错误一直说“无法删除目录:
- java - 我知道 Java Reflection 很慢,但是为什么它的执行时间如此不稳定呢?
- php - php-fpm:mods-available/xdebug.ini 中的设置无效
- tron - 如何在 tron 中设置节点和 cli?
- laravel - 如何用函数在 Laravel eloquent 中发送一个单独的数组?
- python - 熊猫数据框中的日期转换