Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help fixing/refining a while loop sum calculator in python
    primarykey
    data
    text
    <p>Keep in mind that I am still very new to python coding as I am only just into chapter 5 of my python coding class. Keeping that in mind, I am attempting to create a sum calculator using a "while loop" to continue until the user enters a negative number instead of a positive number. </p> <p>In case I am not entirely clear in my description of my question I will post the exact homework problem here: </p> <p>Chapter 5, page 200, #8 Sum of Numbers</p> <p>Write a program with a while 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.</p> <p>Now for the code that I have written so far:</p> <pre><code>def main(): number = float(input('Please enter in a positive number: ')) while number &gt; 0: positiveNumber() while number &lt; 0: calculateTotal() printTotal() def positiveNumber(): number = float(input('If you are finished please enter a negative number.' + \ 'Otherwise, enter another positive number: ')) while number &gt; 0: positiveNumber() while number &lt; 0: calculateTotal() printTotal() def calculateTotal(): total = 0 + number def printTotal(): print('The sum of your numbers is: ', total) main() </code></pre> <ul> <li>In line 11, I have the "+ \" sign there because I wanted to make an enter space there in order to have a cleaner looking text, but that does not seem to work.</li> </ul> <p>I apologize if this question seems "nooby" but I need help making a cleaner/working sum calculator. I would greatly appreciate it if someone can take a look at this code and hopefully help me improve it. Thank you!</p> <p>Final Edit:</p> <p>Thank you all for the informative answers! I learned alot (for a "newbie" =]). I used Talon876's answer for my calculator. Thanks again everyone!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload