age=input("請輸入你的年齡")
if age<18: #注意if后面的語句要+冒號:
print("bukeyi")
else:
print("keyi")
如上所示會報錯,TypeError: '<' not supported between instances of 'str' and 'int'
是因為Input所返回的值是str型的,不能直接與證書比較,故需要將str換成整數(shù)型
添加代碼
ages = int(age)
修改后代碼如下:
age=input("請輸入你的年齡")
ages=int(age)
if ages<18:
print("bukeyi")
else:
print("keyi")
成功運(yùn)行