Home > Software design >  How to compare single variable in a for loop?
How to compare single variable in a for loop?

Time:06-22

I made this program which takes input from user in a for loop and I have to compare and find highest and lowest among the input and on which day. I have successfully calculated average temperatures but somehow I am unable to calculate highest and lowest temperature.

NUMS = 4
htotal = 0
ltotal = 0
htemp = 0
ltemp = 0
f = 0
e = 0

print(">---=== Python Temperature Analyzer ===---<")
for i in range(NUMS):
    i = i   1
    high = int(input("Enter the high value for day %d :" %i))
    low = int(input("Enter the low value for day %d:" %i))
    if low < high and high < 41 and low > -41:
        htemp = htemp high
        ltemp = ltemp low
        htotal = htotal   high
        ltotal = ltotal   low
        print(htotal, ltotal)
        if htemp > high :
            htemp = high
            f = i   

        if ltemp > low:
            ltemp=low
            e = i


    else:
        print("> Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low. <")
        high = int(input("Enter the high value for day %d :" %i))
        low = int(input("Enter the low value for day %d:" %i ))
        htotal = htotal   high
        ltotal = ltotal   low

avghlow = ltotal/4
avghhigh = htotal/4
avgmean=(htotal ltotal)/8


print("\nThe average (mean) LOW temperature was :",avghlow  )
print('The average (mean) HIGH temperature was: ',avghhigh)
print('The average (mean) temperature was : ', avgmean)
print('The highest temperature was ' , htemp,', on day ', f)
print('The lowest temperature was ' , ltemp,', on day ', e)

CodePudding user response:

You've got three problems:

  1. You are updating htemp/ltemp as if they're the totals, not the max and min seen so far
  2. You have your test reversed for htemp vs. high
  3. You have bad initial values for htemp and ltemp

Fixed code (plus some minor PEP8 fixes and tweaks for style) with comments:

NUMS = 4
htotal = 0
ltotal = 0
htemp = -float('inf')  # Initial highest high is lowest possible number so it's replaced immediately
ltemp = float('inf')   # Reversed for lowest low
f = 0
e = 0

print(">---=== Python Temperature Analyzer ===---<")
for i in range(1, NUMS 1):  # Remove need to constantly add 1 by having the range do it for you
    # Removed accumulating in htemp/ltemp
    high = int(input("Enter the high value for day %d :" %i))
    low = int(input("Enter the low value for day %d:" %i))
    if -41 < low < high < 41:  # Python chained conditionals simplify this test a lot
        htotal  = high
        ltotal  = low
        print(htotal, ltotal)
        if htemp < high:  # Flipped test so we replace only when we find a new highest high
            htemp = high
            f = i   

        if ltemp > low:
            ltemp=low
            e = i
    else:
        print("> Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low. <")
        high = int(input("Enter the high value for day %d :" %i))
        low = int(input("Enter the low value for day %d:" %i ))
        htotal = htotal   high
        ltotal = ltotal   low
        # Some work needed here left as an exercise

# Replace magic numbers with NUMS
avghlow = ltotal / NUMS
avghhigh = htotal / NUMS
avgmean = (htotal   ltotal) / (NUMS * 2)


print("\nThe average (mean) LOW temperature was :", avghlow)
print('The average (mean) HIGH temperature was: ', avghhigh)
print('The average (mean) temperature was : ', avgmean)
print('The highest temperature was ', htemp, ', on day ', f)
print('The lowest temperature was ', ltemp, ', on day ', e)

CodePudding user response:

Why hassle with the if loop in the first place? Just create a list and append it every loop. With the builtin min() and max() functions you can get your highest and lowest value. For the means you can simply add all values and divide by the list length.

CodePudding user response:

Here is a modified version of your code that does what I think you are trying for:

NUMS = 4
htotal = 0
ltotal = 0
htemp = 0
ltemp = 0
f = 0
e = 0

print(">---=== Python Temperature Analyzer ===---<")
for i in range(1, NUMS   1):
    firstAttempt = True
    while firstAttempt or not (low < high and high < 41 and low > -41):
        if not firstAttempt:
            print("> Incorrect values, temperatures must be in the range -40 to 40, high must be greater than low. <")
        high = int(input("Enter the high value for day %d :" %i))
        low = int(input("Enter the low value for day %d:" %i))
        firstAttempt = False
    htotal  = high
    ltotal  = low
    print(htotal, ltotal)
    if htemp < high :
        htemp = high
        f = i   

    if ltemp > low:
        ltemp=low
        e = i

avghlow = ltotal / NUMS
avghhigh = htotal / NUMS
avgmean = (htotal   ltotal) / (2 * NUMS)

print("\nThe average (mean) LOW temperature was :",avghlow  )
print('The average (mean) HIGH temperature was: ',avghhigh)
print('The average (mean) temperature was : ', avgmean)
print('The highest temperature was ' , htemp,', on day ', f)
print('The lowest temperature was ' , ltemp,', on day ', e)

Explanation:

  • The range() arguments in the for statement have been updated to start from 1
  • The if/else logic has been replaced with a while loop that repeatedly queries the user at each iteration over i for input until valid input is provided
  • The comparison of htemp and high is fixed (you had >, but needed <)
  • The denominators in your calculations after the loop (avghlow, avghhigh and avgmean) have been changed to use NUMS for consistency.
  • Related