Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 while loop on a calculator missus_brown Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-10-2019, 05:57 PM (This post was last modified: Feb-10-2019, 05:59 PM by missus_brown. Edited 2 times in total.) I'm super new to Python and I'm really having a hard time with this assignment. The previous assignment was a basic calculator. This assignment builds on that calculator by adding a while loop and adding the option to quit. The calculator is supposed to repeatedly loop back to the beginning and ask for a new character until the user types Q to quit. The first basic calculator was working perfectly, but adding the while statement changed that. Here's what I've got. It doesn't work right and it doesn't like the break at the bottom. I'm grateful for any guidance. ```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 %.') char = input('Enter a character to indicate which operation to perform or type Q or q to quit: ') #putting this here leaves it out of the while loop, but I can't put it after 'while' because it has to be defined first. while char != 'Q' or 'q': #is it better to start with char == 'q' or 'Q'? Or something entirely diffent? 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 %.') 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 ```Thanks in advance for any help! Larz60+ aetate et sapientia Posts: 7,322 Threads: 285 Joined: Sep 2016 Reputation: 261 Likes received: 1024 #2 Feb-10-2019, 06:21 PM (This post was last modified: Feb-10-2019, 06:25 PM by Larz60+. Edited 1 time in total.) 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') ``` nilamo likes this post missus_brown Programmer named Tim Posts: 6 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #3 Feb-10-2019, 06:41 PM It works! Also, what does 'char.lower()' do? I've been pulling my hair out on this one. Thank you so much!! ichabod801 Bunny Rabbit Posts: 3,336 Threads: 70 Joined: Sep 2016 Reputation: 176 Likes received: 1028 #4 Feb-10-2019, 08:19 PM `char.lower()` returns the text of char in lower case. This allows you to test against 'q' and 'Q' with one test. This gets past this problem: ```while char != 'Q' or 'q': ```which is a misuse of the 'or' operator. It is equivalent to: ```while (char != 'Q') or 'q': ```Since non-empty strings evaluate as True, 'q' is always True and the loop condition is always True. Also, break can't be used outside of a loop, that's why it was working for you. nilamo likes this post Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Help with while loop creating an infinite loop. FWendeburg 3 313 Jan-30-2019, 08:28 PM Last Post: FWendeburg Area, Perimeter Calculator. Help Pls. NZedMarine 10 801 Nov-13-2018, 11:13 PM Last Post: ichabod801 i need help in fixing my scientific calculator coding : (, im using python 3.5.5 hans77 1 616 Oct-17-2018, 03:26 AM Last Post: stullis simple calculator shakeelthomas 11 1,283 Jul-23-2018, 10:47 PM Last Post: shakeelthomas Investment calculator beardedkiwi 2 1,665 Apr-16-2018, 11:58 AM Last Post: snippsat python age calculator need to find the number of years before they turn 100 not using orangevalley 4 1,322 Mar-26-2018, 04:44 AM Last Post: PyMan Making a calculator with just the + sign gerardmanken 1 891 Feb-01-2018, 03:18 PM Last Post: buran command line calculator Zatoichi 4 1,100 Jan-28-2018, 09:01 PM Last Post: egslava GUI Tip Calculator djkraft 1 1,517 Nov-16-2017, 10:54 PM Last Post: DeaD_EyE Weight loss calculator loop error drogers10940 2 2,041 Oct-18-2017, 08:57 PM Last Post: learner

Forum Jump:

Users browsing this thread: 1 Guest(s)