0

i am doing a program where i enter a number in a while loop and display an error if the sum of the numbers exceed 100.

here's my code

i = 0
while i < 2:
    numbers = float(input('Enter number: '))
    numbers += numbers
    print(numbers)
    limit = 101
    i += 1
    if numbers >= limit:
        print("Over 100")
    else:
        print("Working")

The output is

Enter number: 30.9
61.8
Working
Enter number: 61.9
123.8
Over 100

The number is 61.8 . it added 30.9 + 30.9 immediately and i know is the numbers += numbers does that calculation. Is there a way to do the math properly?

Some Guy
  • 23
  • 6

3 Answers3

1
numbers += numbers

is equivalent to numbers = numbers *2

What you want is to keep a rolling sum. So instead have two variables. One to get input from the user and another to add that input to.

e.g.

sum = 0 
...
numbers = float(input('Enter number: '))
sum += numbers
Erich
  • 1,902
  • 1
  • 17
  • 23
  • let's say i put the code inside a def functionName(): , and the sum = 0 is outside the function . how do i call the sum into the function? – Some Guy Aug 24 '17 at 17:39
  • you shouldn't. Instead you should return the value that the function determines and assign it to some variable if you want to save it. If you want to pass a variable to a function in python you can define it as a global (bad) and you can put it into a list or dictionary or object and pass that and read it (also not good for this use case). see: https://stackoverflow.com/questions/13299427/python-functions-call-by-reference – Erich Aug 24 '17 at 17:42
0

I'm confused on the issue. Your output looks correct. Maybe you are looking for numbers = float(input('Enter number: '))*2 ?

Also, you should probably make limit 100 and check if numbers is greater than limit. Right now, 100.5 in your code will print "Working" when it should print "Over 100"

Treyten Carey
  • 641
  • 7
  • 17
  • Now that I see Erich's reply, I see maybe that's what you were looking for. – Treyten Carey Aug 24 '17 at 17:40
  • ah , it seem like my question is being misunderstood . i meant the issue is that i want the sum of the numbers (input) and add themselves together to check if the sum is exceeded limit(100) – Some Guy Aug 24 '17 at 17:41
-1
i = 0
limit = 101

total_number = 0
while i < 2:
    numbers = float(input('Enter number: '))
    total_number += numbers
    print(total_number)

    i += 1

if total_number >= limit:
    print("Over 100")
else:
    print("Working")
Dharmesh Fumakiya
  • 2,276
  • 2
  • 11
  • 17
  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) – Patrick Aug 25 '17 at 00:06