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) 
3
  • 2
    Please copy the code directly into the question, instead of linking to a screenshot. Commented Sep 13, 2018 at 20:17
  • 3
    I really have no idea what you're asking in your question. What are you trying to do? Commented Sep 13, 2018 at 20:18
  • Can you please explain what you expect to happen, and what happens instead? Commented Sep 13, 2018 at 20:22

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

9 Comments

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.
So you do want to exit if a negative number is found?
Well if its negative, I want the program to end there and compute only the positive values
@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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.