Here is an example of a simple calculator program using the while loop in Python.
Example: Python Calculator Using While Loop
- The code starts by declaring a variable called sum1 and initializing it to 0.
- The code then goes on to loop through the input() function, which takes in an integer as its argument.
- If the user enters "stop" into the input box, then print out the value of sum1 and break from the loop.
- Otherwise, add one to sum1 each time they enter a number into their input box until they enter "stop".
- The code will continue to loop until the user enters a "stop" command.
sum1 = 0 while True: x = input() if x == 'stop': print(sum1) break else: sum1 += int(x) print("Finished")
Output:
87 9 76 stop 172 Finished