jeudi 7 septembre 2017

Python. Incorrect result if elif

I am a fairly new beginner to python. I wrote a code with subroutines to find the largest number and smallest number and the range between the numbers. However the answer to my test data comes up wrong while I am trying to find the largest number. Test data x=12, y=6, z=2 shows that the largest number is y.

x = input("Enter x:")
y = input("Enter y:")
z = input("Enter z:")
if x>y and x>z :
    a = 'x is the largest'
    c1 = x
elif (y>x and y>z):
    a = 'y is the largest'
    c1 = y
elif (z>x and z>y):
    a = 'z is the largest'
    c1 = z
else :
    a ='all numbers are equal'
    c1 = 0
if x<y and x<z :
    b = 'x is the smallest'
    c2 = x
elif (y<x and y<z):
    b = 'y is the smallest'
    c2 = y
elif (z<x and z<y):
    b = 'z is the smallest'
    c2 = z
else :
    b = 'all numbers are equal'
    c2 = 0
c = int(c1)-int(c2)
d = "Bye" 
print("[1] Find the highest variable.")
print("[2] Find the lowest variable.")
print("[3] Find the range between the highest and the lowest variables.")
print("[q] Quit.")
while d == ('Bye'):
    choice = input("Enter choice number:")
    if choice == '1'   :print (a)
    elif choice == '2' :print (b)
    elif choice == '3' :print (c)
    elif choice == 'q' :
    print (d)
    d="end"
    input ()

Aucun commentaire:

Enregistrer un commentaire