0

I am trying to find the maximum and minimum without using the min and max functions. But the maximum is only displaying the first number. Any help?

My code:

count = 0
total = 0.0

num = float(input("Enter the number: ")) 
maximum = num
minimum = num

while num = 0:
    count = count + 1
    total = total + num
    num = float(input("Enter the number: "))

if num < minimum:
    minimum = num
else:
    num > maximum
    maximum = num

if count == 0:
    print("Invalid Entry.")
else:
    print("Average Number:", round(total/count, 1))
    print("Minimum Number:", minimum)
    print("Maximum Number:", maximum)
rpanai
  • 12,515
  • 2
  • 42
  • 64
TJH
  • 11
  • 3
  • 1
    I'm guessing the condition is `while num != 0:`? On the other hand, you should compare inside the `while` loop. Also, take a closer look at the `else:`, it should be `elif num > maximum:`. – dcg Mar 31 '20 at 13:12
  • 1
    This code snippet probably doesn't work. In any case, I recommend using a debugger to see where the code leads you. For instance, my guess is that you meant to indent the line `if num < minimum`, and a debugger will show you don't reach it in the loop. – NadavS Mar 31 '20 at 13:13

4 Answers4

1

You did not intended the if condition that is why it is not working
I have modified the code for getting 6 numbers one after one try this

count = 0
total = 0.0

num = float(input("Enter the number: ")) 
maximum = num
minimum = num

while count < 5:
    count = count + 1
    total = total + num
    num = float(input("Enter the number: "))

    if num < minimum:
       minimum = num
    else:
       num > maximum
       maximum = num

if count == 0:
    print("Invalid Entry.")
else:
    print("Average Number:", round(total/count, 1))
    print("Minimum Number:", minimum)
    print("Maximum Number:", maximum)
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
0
count = 0
total = 0.0

num = None 
maximum = -float("inf")
minimum = float("inf")
# No need to do the 1st round outside the loop
while num != 0:
    num = float(input("Enter the number: "))
    count += 1
    total += num  # += is more concise

    # This block needs to be in the while loop
    if num < minimum:
        minimum = num
    if num > maximum:  # elif is fine if you initialize minimum and maximumwith the 1st value of num
        maximum = num

if count == 0:
    print("Invalid Entry.")
else:
    print("Average Number:", round(total/count, 1))
    print("Minimum Number:", minimum)
    print("Maximum Number:", maximum)
gdelab
  • 6,124
  • 2
  • 26
  • 59
0

I'm not quite sure what you are trying to accomplish here. If your first input is anything other than 0, you will set the variables maximum and minimum to that non-zero number. You will skip the while loop and if statement. If you don't get an error, it will probably just spit out the maximum and minimum being the same number. Can you provide more detail as to what you are trying to do?

BigHeadEd
  • 75
  • 1
  • 1
  • 8
0

you need to check the minimum and maximum for every number. so this part must be in while loop:

if num < minimum:
  minimum = num

if num > maximum
  maximum = num

also while condition doesn't seem right. you are assigning 0 to num every time. you probably meant num!=0. in this case when user inputs 0 the program terminates.

Mahshid
  • 71
  • 3