Python Forum
while loop on a calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop on a calculator
#2
Better to start thusly:
print('This program is a basic calculator that allows you to perform calculations using two numbers and a character that indicates which math operation to perform. Options for the characters are + - * / and %.')

while True:
    char = input("Enter q to Quit, otherwise operator (+, -, * /): ")
    
    if char.lower() == 'q':
        break

    if char not in '+-*/':
        print('Invalid entry')        
        continue
    ...
also change at bottom
else:
    print('Goodbye')
    break #this break isn't allowed here because it's after 'else' but I can't figure out where else to put this part
To (no indentation):
print('Goodbye')
Final code should look like:
print('This program is a basic calculator that allows you to perform calculations using two numbers and a character that indicates which math operation to perform. Options for the characters are + - * / and %.')

while True:
    char = input("Enter q to Quit, otherwise operator (+, -, * /): ")
    
    if char.lower() == 'q':
        break

    if char not in '+-*/q':
        print('Invalid entry')        
        continue

    a = float(input('Enter a number: ')) 
    b = float(input('Enter another number: ')) 
 
    if char == '+': 
        sum = a + b 
        print('The sum of ',a, ' and ', b,' is ', sum , sep = '')
 
    elif char == '-': 
        difference = a-b 
        print(a, ' minus ', b, ' equals ', difference, '.', sep = '')
 
    elif char == '*':  
        product = a*b  
        print(a, ' times ', b, ' equals ', product, '.', sep='')
 
    elif char == '/': 
 
        if b == 0: 
            print('You can not divide a number by zero.')
 
        else: 
            quotient = a/b 
            rounded = format(quotient, '.2f') 
            print(a, ' divided by ', b, ' equals ', rounded, '.', sep = '')
 
    elif char == '%': 
        remainder = a%b 
        print('The remainder of ',a, ' and ', b, ' is ', a%b, '.', sep='') 
 
 
    else:
        print('That is not a valid character. Please enter + - * / or %.')

print('Goodbye')
Reply


Messages In This Thread
while loop on a calculator - by missus_brown - Feb-10-2019, 05:57 PM
RE: while loop on a calculator - by Larz60+ - Feb-10-2019, 06:21 PM
RE: while loop on a calculator - by missus_brown - Feb-10-2019, 06:41 PM
RE: while loop on a calculator - by ichabod801 - Feb-10-2019, 08:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculator Loop Help dock1926 3 3,877 Jun-23-2021, 10:05 PM
Last Post: HereweareSwole
  Python calculator help but not using while loop with true, any flags variable ,break kirt6405 13 5,781 Jun-08-2021, 06:39 AM
Last Post: Larz60+
  Weight loss calculator loop error drogers10940 7 14,553 Feb-14-2021, 12:36 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020