5 Replies - 5074 Views - Last Post: 29 March 2012 - 11:55 AM Rate Topic: -----

#1 4jzbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 12

Sum of Numbers

Posted 14 March 2012 - 06:52 PM

Hi all,
I am working on this Python assignment and it doesn’t work. Please I need your help. Thanks.

Write a program with a loop that asks the user to enter a series of positive numbers. The
user should enter a negative number to signal the end of the series. After all the positive
numbers have been entered, the program should display their sum.

Here is what I have so far:

Here is what I have so far:
# main function
def main():
# Local variables
    number = 1.0 #initialize for while loop
    total = 0.0
	#continue adding numbers while they are positive
    while number > 0:
        number = float(input("Enter a positive number (negative to quit): "))
	   #check that number is positive so as not to change value
	   #of the total
        if number > 0:
            total = total + number
    #Display total
    print ("Total = ", format(total, '.2f'))



Is This A Good Question/Topic? 0
  • +

Replies To: Sum of Numbers

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Sum of Numbers

Posted 14 March 2012 - 06:56 PM

Aside from some aesthetics, it works fine. What problem are you having?
Was This Post Helpful? 1
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Sum of Numbers

Posted 14 March 2012 - 07:25 PM

I concur with Motoma.

This post has been edited by atraub: 14 March 2012 - 07:26 PM

Was This Post Helpful? 0
  • +
  • -

#4 4jzbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 12

Re: Sum of Numbers

Posted 15 March 2012 - 01:22 PM

So far so good... It does work!
Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Sum of Numbers

Posted 15 March 2012 - 05:41 PM

lol I'm glad it works boss. Next time, please test to make sure your code is actually broken before you post about it.
Was This Post Helpful? 0
  • +
  • -

#6 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Sum of Numbers

Posted 29 March 2012 - 11:55 AM

Proly didnt work because you didnt call your main function at the end of the program
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1