python - python 程序中某些值的输出结果显示不正确
问题描述
我在 Visio 代码中编写了一个 python 程序,我发现了一个奇怪的行为
代码:
a=input("Enter Jim age ")
b=input("Enter Michal age ")
c=input("Enter sunny age ")
d=input("Enter Anders age ")
if(a>b):
print("Jim is elder")
if(b>c):
print("Michal is elder")
if(c>d):
print("sunny is elder")
else:
print("nothing is true")
现在,当我输入值时13,11,9,8
,它只会显示"Jim is elder sunny is elder"
不正确的值。但是如果我输入输入值20,18,16,14
,它将正确显示所有结果。当我输入时,同样的错误结果也会显示,11,9,8,7
在这种情况下输出是" Michal is elder sunny is elder"
.
解决方案
int
在比较它们之前,您需要将输入字符串转换为:
a= int(input("Enter Jim age "))
b= int(input("Enter Michal age "))
c= int(input("Enter sunny age "))
d= int(input("Enter Anders age "))
if(a>b):
print("Jim is elder")
if(b>c):
print("Michal is elder")
if(c>d):
print("sunny is elder")
else:
print("nothing is true")
推荐阅读
- heroku - 查看 Heroku 上的旧日志
- python - 如何使用python和excel计算器
- javascript - 在Angular 6运行时重新分配复杂对象内的值
- javascript - 异步代码运行 - 在 react-native 中使用 Javascript OOP
- python - 为什么这个循环不会按照语句的顺序重复?
- javascript - 如何使 toLowerCase() 和 toUpperCase() 在浏览器之间保持一致
- c - C 语言乘法程序无法正常工作
- jmeter - JMeter 脚本记录问题 - 代理工作,但没有记录
- php - mysql的到达日期和离开约束(检查约束)
- excel - 按大纲级别为 Excel 行着色