1

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, smallest number and even the range.. Test data x=12, y=6, z=2 shows that the largest number is y.

note: variables are output1 for what choice 1 prints and so on

x = input("Enter x:")
y = input("Enter y:")
z = input("Enter z:")
if x>y and x>z :
 output1 = 'x is the largest'
 large = x
elif (y>x and y>z):
 output1 = 'y is the largest'
 large = y
elif (z>x and z>y):
 output1 = 'z is the largest'
 large = z
else :
 output1 ='all numbers are equal'
 large = 0
if x<y and x<z :
 output2 = 'x is the smallest'
 small = x
elif (y<x and y<z):
 output2 = 'y is the smallest'
 small = y
elif (z<x and z<y):
 output2 = 'z is the smallest'
 small = z
else :
 output2 = 'all numbers are equal'
 small = 0
output3 = large-small
outputq = "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 outputq == ('Bye'):
 choice = input("Enter choice number:")
 if choice == '1' :print (output1)
 elif choice == '2' :print (output2)
 elif choice == '3' :print (output3)
 elif choice == 'q' :
 print (outputq)
 outputq="end"
 input ()
6
  • 2
    FYI: "Subroutines" are commonly called functions in Python. But actually, I don't see any functions in your code. Commented Sep 7, 2017 at 18:19
  • I meant 3 different purposes as in Lowest variable /Highest variable / Range Commented Sep 7, 2017 at 18:22
  • stackoverflow.com/questions/20449427/… stackoverflow.com/questions/4915361/… Commented Sep 7, 2017 at 18:22
  • Note that the fact that there is no single number bigger or smaller than the other two does by no means imply that all number are equal. It just implies that the two biggest or the two smallest numbers are equal. Commented Sep 7, 2017 at 18:28
  • @SvenMarnach I dont understand.If the two smallest numbers or largest number are equal the code will ignore it and output the smallest / bigger number Commented Sep 7, 2017 at 18:47

2 Answers 2

1

if x>y and x>z : in your code compares strings, not numbers, because input() returns string. Convert it to int:

x = int(input("Enter x:"))
y = int(input("Enter y:"))
z = int(input("Enter z:"))
answered Sep 7, 2017 at 18:22
Sign up to request clarification or add additional context in comments.

4 Comments

Let's hope the OP is using Python 3, but given the behaviour they are observing it seems likely.
python3 I think, because print()
Yeah, I noticed, but it's all print() with just a single argument, so it would do the same thing in Python 2.
I am using Python 3
0

I have restructured your code and divided into functions

First find largest

def find_largest(data):
 x, y, z = data
 print('\nx = {}, y = {}, z = {}'.format(x,y,z))
 a=''
 if x>y and x>z :
 a = 'x is the largest'
 elif (y>z):
 a = 'y is the largest'
 else:
 a = 'z is the largest'
 return a

Find Lowest

def find_lowest(data):
 x, y, z = data
 print('\nx = {}, y = {}, z = {}'.format(x,y,z))
 b = ''
 if x<y and x<z :
 b = 'x is the smallest'
 elif (y<z):
 b = 'y is the smallest'
 else:
 b = 'z is the smallest'
 return b

Find Mid range

def find_mid(data):
 return (max(data)-min(data))

Final, code snippet

data = [12,6,2] # used list to pass data
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.")
d = ''
while d != 'Bye':
 choice = input("Enter choice number:")
 if choice == '1' :
 print (find_largest(data))
 elif choice == '2' :
 print (find_lowest(data))
 elif choice == '3' :
 print (find_mid(data))
 elif choice == 'q' :
 d = 'Bye'
 print(d)

Check the working code on below link

Find Largest/lowest and mid

This link will help you to understand the flow of your code. Click on forward button to understand the steps.

answered Sep 7, 2017 at 19:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.