如果不工作

Neh*_*hal 1 python

我正在尝试创建一个简单的程序,我要求用户放置name,age然后使用if else打印结果.

我希望什么时候放到20岁,所以结果应该来

祝贺你的年龄是20,你可以找到真相

但这里不起作用的是我正在使用的代码.

name = input("what is your name \n")
print('Nice to meet you', name, "!!")
age = input("Your Age ? \n")

if age > 18:
    print('Congratulation ' + name + ' your age is '+ str(age), ' and you can find the truth ')
else:
    print('Sorry ' + name + 'your age is '+ str(age), ' and you can also find the truth if you start from today')
Run Code Online (Sandbox Code Playgroud)

运行我的代码后我得到的错误.

错误

what is your name 
Edit
Nice to meet you Edit !!
Your Age ? 
20
Traceback (most recent call last):
  File "C:/Users/hp/PycharmProjects/Project/input_user.py", line 5, in <module>
    if age > 18:
TypeError: unorderable types: str() > int()
Run Code Online (Sandbox Code Playgroud)

帮我解决这个问题.

Arc*_*676 5

阅读错误消息:

TypeError: unorderable types: str() > int()
Run Code Online (Sandbox Code Playgroud)

您正在尝试比较字符串和整数.您必须将输入转换为int第一个:

age = int(input("Your Age ? \n"))
Run Code Online (Sandbox Code Playgroud)

这将允许您将其与数字进行比较.

或者,在需要与数字进行比较时进行转换

if int(age) > 18:
Run Code Online (Sandbox Code Playgroud)

但现在这更像是一个可用性问题.最好将它简单地转换int为一旦你得到它,因为年龄你可能需要的数值超过字符串值.