50,838 views
24 votes
24 votes
write a program to input 3 numbers and print the largest and the smallest number without using if else statement​

User Daniel Zhang
by
2.8k points

1 Answer

14 votes
14 votes

Answer:

def main():

# input

num1 = int(input("Type in a number: "))

num2 = int(input("Type in another number: "))

num3 = int(input("Type in another number: "))

list1 = [num1, num2, num3]

# sorts the array

list1.sort()

# list1[-1] prints the first element in the array

print("The largest number is: " + str(list1[-1]))

# -len(list1) takes the length of the list, in this case 3, and makes it a negative number.

# That negative number is then used as an index for the list1 array in order to print the lowest number.

print("The smallest number is: " + str(list1[-len(list1)]))

main()

Step-by-step explanation:

Hope this helped :) I left some comments so you know what's going on. You can also use max(list1) and min(list1) but I chose indexing because indexing is the better way of doing stuff like this.

Have a good day!

User Matt Mason
by
2.9k points