0

so I'm fairly new to python or coding in general and was wondering if anyone can help me with this code of mine i'm doing as homework. So, what I'm trying to do is when I type a negative number, I don't want the negative number to compute but I don't know how to go on about it.

Atm, still currently trying to figure out what loops or what not to actually cancel out the negative numbers but my dumbself can't seem to get it :l

totalSum = 0
n = 0
avg = 0
n = int(input('How many numbers you wish to enter? '))
for i in range(n):
   num = eval(input('Enter any number: '))
   if num < 0:
       break
totalSum += num
avg = totalSum / n

print('Total: ', totalSum, '| ' 'Average: ', avg)
Kendo
  • 13
  • 3

1 Answers1

0
totalSum = 0
avg = 0
mean = 0
n = int(input('How many numbers you wish to enter? '))
for i in range(n):
    num = eval(input('Enter any number: '))
    if num < 0:
        continue
    else:
        mean += 1
        totalSum += num
avg = totalSum / mean

print('Total: ', totalSum, '| ' 'Average: ', avg)

I think this is what you're looking for. You want to find the negative number, and ignore it, but if the number isn't negative, then you want to add it into the totalSum

Output:

How many numbers you wish to enter? 5
Enter any number: 1
Enter any number: 2
Enter any number: -5
Enter any number: -5
Enter any number: -5
Total:  3 | Average:  1.5
J0hn
  • 570
  • 4
  • 19
  • Well the question asks to enter any number of non-negative floating-point values and if there is a negative value, the program terminates. Then I gotta find the sum, mean, max, and min but the negative values are not used when computing. – Kendo Sep 13 '18 at 20:22
  • So you *do* want to exit if a negative number is found? – J0hn Sep 13 '18 at 20:24
  • Well if its negative, I want the program to end there and compute only the positive values – Kendo Sep 13 '18 at 20:26
  • @Kendo Then the above code should do what you want, unless you want it to ignore anything after the negative number, in which case you'll need to modify it a bit more. – J0hn Sep 13 '18 at 20:27
  • Well lets say i want to enter 3 numbers, so the first number is 50, second is 4, and third is -45, The total will be 54 since it's not counting the negative integer. However, since I want 3 numbers, when i get the average its including the 3rd number. – Kendo Sep 13 '18 at 20:31
  • See my updated above code, moving the avg calculation outside of the loop should fix this, I also added a `mean` counter that only iterates up if it's a non-negative number. You could probably also iterate `n` down if `num < 0:` – J0hn Sep 13 '18 at 20:37
  • AYYYE THANK YOU SO MUCH :D After spending a sad week just on this one.. Thank you so much! :) also, quick question? what does mean += 1 do or why is it mean += 1? – Kendo Sep 13 '18 at 20:43
  • @Kendo `x += 1` is the same as `x = x + 1` it's adding 1 to whatever the variable was before. So an alternate solution to this would be to do `n -= 1` in the `if num < 0:` part of the code, and then `avg = totalSum / n` should give the same result. – J0hn Sep 13 '18 at 20:45
  • I've managed to get the max and min just by creating a list :) and then from there I just append the num :> – Kendo Sep 14 '18 at 00:08