首页 > 解决方案 > 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".

标签: pythonpython-3.x

解决方案


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")

推荐阅读