-1
import time

total = 0
pos = 0
zeroes = 0
neg = 0

print('This program will add any seven numbers for you')
time.sleep(2)
print()
a = int(input('Please enter the first number: '))
total = total + a
if a > 0:
    pos = pos + 1
elif a == 0:
    zeroes = zeroes + 1
elif a < 0:
    neg = neg + 1


time.sleep(2)


b = int(input('Please enter the second number: '))
total = total + b
if b > 0:
    pos = pos + 1
elif a == 0:
    zeroes = zeroes + 1
elif  a < 0:
    neg = neg + 1


time.sleep(2)


c = int(input('Please enter the third number: '))
total = total + c
if c > 0:
    pos = pos + 1
elif c == 0:
    zeroes = zeroes + 1
elif c < 0:
    neg = neg + 1


time.sleep(2)


d = int(input('Please enter the fourth number: '))
total = total + d
if d > 0:
    pos = pos + 1
elif d == 0:
    zeroes = zeroes + 1
elif d < 0:
    neg = neg + 1


time.sleep(2)


e = int(input('Please enter the fifth number: '))
total =total + e
if e > 0:
    pos = pos + 1
elif e == 0:
    zeroes = zeroes + 1
elif e < 0:
    neg = neg + 1


time.sleep(2)


f = int(input('Please enter the sixth number: '))
total = total + f
if f > 0:
    pos = pos + 1
elif f == 0:
    zeroes = zeroes + 1
elif f < 0:
    neg = neg + 1


time.sleep(2)


g = int(input('Please enter the seventh number: '))
total = total + g
if g > 0:
    pos = pos + 1
elif g == 0:
    zeroes = zeroes + 1
elif g < 0:
    neg = neg + 1


time.sleep(2)

print()
print('The sum of your entries is: ', + total)


time.sleep(2)
print()

print('You entered', + pos, 'positive numbers')


time.sleep(2)
print()

print('You entered', + zeroes, 'zeroes')

time.sleep(2)
print()

print('You entered', + neg, 'negative numbers')
print()

time.sleep(3)

Hello! I have the variable 'neg' keeping a running total of all of the negative numbers that the user enters. It seems as if the negative numbers aren't always being added to the 'neg' running total at the end of the code. I've been working with Python 3x for about a week now, so be gentle :) Thanks in advance for the help!

Edit: I have reworked this into a (working) loop per the advice of Kevin, is this a good loop? It seems to work, I'm just looking for pointers as I'm kind of struggling with Python logic. Big thanks to Kevin, wish I could upvote you!

New code posted below:

import time

sums = 0
pos = 0
neg = 0
zero = 0
numb = 0
user_numb = 0

running = True

print('This program will add any 7 numbers for you')
time.sleep(.5)
print()

while running:
    user_numb = int(input('Please enter a number: '))
    sums = user_numb + sums
    numb = numb + 1
    print()
    if user_numb > 0:
        pos = pos + 1
    elif user_numb < 0:
        neg = neg + 1
    elif user_numb == 0:
        zero = zero + 1
    if numb == 7:
        running = False

print()
time.sleep(2)
print('The sum of the numbers entered was: ', + sums)
print()
time.sleep(2)
print('You entered', + pos, 'positive numbers')
print()
time.sleep(2)
print('You entered', + neg, 'negative numbers')
print()
time.sleep(2)
print('You entered', + zero, 'zeroes')
print()
print()
time.sleep(3)
  • You made a transcription error when copying and pasting. There's at least one place where you incorrectly used `a` instead of some other letter. You should rewrite this whole thing as a loop or a function, so that this kind of error cannot happen. – Kevin Apr 17 '17 at 05:24
  • Thank you! I actually typed all of this out while looking at the code above each if statement, that's a real duh moment. This was supposed to be a loop in the tutorial I'm doing, it's one of the labs. I'm going to re read that section and redo the whole thing as a loop. Thanks a million! – DrProfessor85 Apr 17 '17 at 05:45
  • @Kevin What do you think of the new code? Do you have any pointers regarding loops? Thanks again! – DrProfessor85 Apr 17 '17 at 07:24

1 Answers1

0

In the section with b you forgot in the copy/pasted code to change all 'a' into 'b' and have still twice an 'a' there:

b = int(input('Please enter the second number: '))
total = total + b
if b > 0:
    pos = pos + 1
elif a == 0: # CHANGE TO 'b'
    zeroes = zeroes + 1
elif  a < 0: # CHANGE TO 'b'
    neg = neg + 1

so you see wrong results only in case the second number is a 0 or negative.

Claudio
  • 3,417
  • 2
  • 10
  • 33
  • Thank you! Could you take a look at the new code I posted on this question and let me know if you have any pointers? I'm having a rough go with Python logic. – DrProfessor85 Apr 17 '17 at 07:29
  • I would replace `while running` with `for _ in range(7)' then you don't need the `numb` counter anymore and you can see directly in the loop header how often it will be repeated. – Claudio Apr 17 '17 at 07:35
  • Well that does seem easier than using a boolean value so I could scrap that part. Thanks! – DrProfessor85 Apr 17 '17 at 07:42
  • the `_` is a general purpose "throwaway" variable name see here: http://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python . It's used because you don't need the counter value in your loop, but if you need a counter in a loop use for example `i` instead of `_`. – Claudio Apr 17 '17 at 07:44