Home > Net >  For loop shows the wrong maximum value
For loop shows the wrong maximum value

Time:06-27

a,b,c = int(input("Type value a ")), int(input("Type value b ")), int(input("Type value c "))
    
for num in a,b,c:
    if num >= b and num >= c:
        max_num = a
    elif num >= a and num >=c:
        max_num = b
    else:
        max_num = c
print(f'The max number is {max_num}')  

The output doesn't add up. for example here's a sample output:

Type value a 100
Type value b 20
Type value c 10
The max number is 10

I would like to know the mistake I've made in how num in a,b,c is iterated. To elaborate, in my mind, the for loop takes the first number (num) a and then compares it to b and c. If it's less, then it moves on to the elif condition and lastly c. This is repeated for a,b,c.

Explain how I've misunderstood how for loops or if/elif work if that's the issue.

CodePudding user response:

The logic you tried to implement with if..elif should not need a loop. Executing it once with the right value for num, would be enough:

if a >= b and a >= c:
    max_num = a
elif b >= a and b >= c:
    max_num = b
else:
    max_num = c

You can also solve it with a loop, but then you would incrementally improve a previous maximum. So you'd start with assuming a is the greatest, and then check if any of the two others are greater:

max_num = a
for num in b,c:
    if num >= max_num:
        max_num = num

Finally, Python has a function for this:

max_num = max(a, b, c)

CodePudding user response:

This is why I am a proponent of learning a moderately low-level language as your first language. The shortcuttery offered by python often distorts program logic. This would be a typical correct implementation:

a = int(input("Type value a: "))
b = int(input("Type value b: "))
c = int(input("Type value c: "))

max_num = a # not 0

arr = [a, b, c]

print(arr)

for num in arr:
    if max_num < num:
        max_num = num

print (f'The max number is {max_num}') 

You had to:

  • Give a value first (initizalize) to max number. Because otherwise, what would it compare itself to?
  • num in this for loop would be iterating through value inside the array which are a, b and c respectively.
  • max_num would be compared to said value, and if the value if greater, would be assigned as the new max number.

I do not know any python syntax for num in a,b,c but my way highlights the logic clearly. We first solve the problem, then we proceed to optimize and shortcut or what have you.

Hope this helps.

Update: As @trincot has pointed out, if we wish to compare max_value out of a, b and c, we initialize max_value to a, not 0.

CodePudding user response:

These might be some solutions for your problem:

a,b,c = int(input("Type value a ")), int(input("Type value b ")), int(input("Type value c "))
max_num = -1
for num in a,b,c:
    if max_num < num:
        max_num = num
print(f'The max number is {max_num}')

or simply

a,b,c = int(input("Type value a ")), int(input("Type value b ")), int(input("Type value c "))
print(f'The max number is {max(a,b,c)}') 

Now for the explanation of your mistake, First there is no involvement of max_num inside your loop other than assignment. For each iteration your checking if the element is a or b or c (although not perfectly) and assigning them to max_num so in the end the max_num contains the value of c

CodePudding user response:

a,b,c = int(input("Type value a" " ")),int(input("Type value b" " ")),int(input("Type value c" " "))
    
for num in a,b,c:
    if num >= b and num >= c:
        max_num = a
    elif (num >= a) and (num >=c) :
        max_num = b
    else:
        max_num = c
    break # use break to correct the code
print (f'The max number is {max_num}') 

your answer is now correct. Use 'Break' to stop the programme.

  • Related