0

I want to find the the minimum and maximum value with loop without using the list function. The code works perfectly I just get the same maximum as minimum value?

import math

Choice= ''
sum=0

while Choice!='3':
    print ('1- Amount of rain per month')
    print('2- See statistic for this year')
    print('3- Finish')
    val= input ('Your choice:')

    if Choice== '1':

        for x in range (1,13):
            rain= input ('Give the number of month'+ str(x)+ ':')
            rain=float(rain)
            sum+=rain
            print('Sum:', sum)

    if val== '2':
        print('The average in a year:', sum/12)
        minVal=0
        minVal<rain
        minVal=rain
        print('Minimum value:', minVal)
        maxVal=0
        rain > maxVal
        maxVal=rain
        print('Maximum value:', maxVal)


if Choice=='3':
        print('Finish')
2
  • ... works perfectly and max==min contradicts itself ... Commented Sep 11, 2019 at 20:41
  • rain > maxVal is a comparison that returns True or False ...but you do nothing with it. You also only have the total sum - not the single month-values stored. You would need to store the values of the months into a list - then you can loop over the list and get the min and max values. Or you define min/max first and on input when new jnumber is bigger/lower adjust your "memorized" min/max-values. Fixing this would mean to rewrite your whole code - you might want to browse some tutorials to get the hang of lists and loops. Commented Sep 11, 2019 at 20:42

2 Answers 2

1
Choice= ''
sum=0
minVal = 0
maxVal = 0

while Choice!='3':
    print ('1- Amount of rain per month')
    print('2- See statistic for this year')
    print('3- Finish')
    Choice = input ('Your choice:')

    if Choice== '1':

        for x in range (1,13):
            rain= input ('Give the number of month'+ str(x)+ ':')
            rain=float(rain)
            sum+=rain
            print('Sum:', sum)
            if minVal >rain or minVal == 0:
                minVal = rain
            if rain > maxVal or maxVal == 0:
                maxVal = rain

    if Choice == '2':
        print('The average in a year:', sum/12)
        print('Minimum value:', minVal)
        print('Maximum value:', maxVal)


if Choice=='3':
        print('Finish')

This is the solution of your problem!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your help :)
@Mahsa I am glad to help you out
@Trenton_M really appreciated!
1

Is it this program that you want?

import math

val= ''
sum=0
count=0
min=0
max=0

while val!='3':
 print ('1- Amount of rain per month')
 print('2- See statistic for this year')
 print('3- Finish')
 val= input ('Your choice:')

 if val== '1':

  for x in range (1,13):
    rain= input ('Give the number of month'+ str(x)+ ':')
    rain=float(rain)
    if count == 0:
      min = rain
      max = rain
      count=1
    else:
      if (min > rain):
          min = rain
      if (max < rain):
          max = rain
    sum += rain
    print('Sum:', sum)

 if val == '2':
     print('The average in a year:', sum / 12)
     print('Minimum value:', min)
     print('Maximum value:', max)
 if val == '3':
     print('Finish')

2 Comments

I tried this code and I still get the same maximum value as minimum value
You can write second codes(starting with if(count==0) ) under the rain=float(rain) ,because It is more.I shared all codes for you as unified .

Your Answer

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.