mi1ntcsci
mi1ntcsci

Reputation: 3

Maximum and Minimum using loops

I had to rearrange code so that when I run the program the output is:

My attempt allowed the output to do everything except print the maximum or the minimum. Have tried moving count = count + 1 outside the loop to no avail. Still a beginner, any help is appreciated. my attempt:

value = int(input("Enter an input value: "))
number = int(input("How many numbers would you like to enter: "))
count = 1
largest = value
smallest = value
while count <= number :
    value = int(input("Enter an input value: "))
    count = count + 1
    if value > largest :
        largest = value
        print("The maximum is: ", largest)
    if value < smallest :
        smallest = value
        print("The minimum is: ", smallest)

expected output:

Enter an input value: 3
How many numbers would you like to enter: 7
Enter an input value: 2
Enter an input value: 9
Enter an input value: 23
Enter an input value: 4
Enter an input value: 1
Enter an input value: 8
Enter an input value: 3
The maximum is:  23
The minimum is:  1

Upvotes: 0

Views: 714

Answers (1)

mozway
mozway

Reputation: 260500

You just need to move the print at the end of your code, only after you have tested all elements:

value = int(input("Enter an input value: "))
number = int(input("How many numbers would you like to enter: "))
count = 1
largest = value
smallest = value
while count <= number :
    value = int(input("Enter an input value: "))
    count = count + 1
    if value > largest :
        largest = value
    if value < smallest :
        smallest = value
print("The maximum is: ", largest)
print("The minimum is: ", smallest)

output:

Enter an input value: 3
How many numbers would you like to enter: 7
Enter an input value: 2
Enter an input value: 9
Enter an input value: 23
Enter an input value: 4
Enter an input value: 1
Enter an input value: 8
Enter an input value: 3
The maximum is:  23
The minimum is:  1

Upvotes: 1

Related Questions