Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fixing the code
#1
Hi everybody, I'm a new beginner and I just starting to learn to code. I have a question about the code that I coded, when I put the operation in and I put two number in, it does not show the result statement but instead it keep doing the while loop, so can you you guys give me any advice?
''' This program is used for calulating numbers as a simple calculator
Created by Khanh Cao
05/02/2020'''
print('This program behaves as a simple calculator. It accpepts 2 numbers and one charater. The character indicates which operation is to be performed')
flag='true'
while True:
    operation=input('Enter one of + - * / % to idicate which operation to perform, or enter q or Q to quit: ')
    if operation =='Q' or operation=='q':
        
        print('Quitting')
        continue
    num1=float(input('Enter the first number: '))
    num2=float(input('Enter the second number: '))
    if operation=='+':
        k=num1+num2
    elif operation=='-':
        k=num1-num2
    elif operation=='*':
        k=num1*num2
    elif operation=='/':
        if num2== 0:
            print ('You can not divide a number by zero')
        else:
            k=num1/num2
    elif operation == '%':
        k=num1%num2
    else:
        flag='false'
        print('Unexpected Operation')
if flag =='true':
    print('The result of', num1, operation,num2,'is: ',k)
Reply
#2
While True:
Doesn't have any way to be set False and terminate the loop.

If flag is what you what to use try:
flag = True
While flag:


While you can us "true" and "false", True and False and there for this purpose.
Reply
#3
using while True: is perfectly alright and better than using a flag. you just need to use break, not continue to break out of the loop.
print('This program behaves as a simple calculator. It accpepts 2 numbers and one charater. The character indicates which operation is to be performed')
 
while True:
    operation=input('Enter one of + - * / % to idicate which operation to perform, or enter q or Q to quit: ')
    if operation == 'Q' or operation == 'q':
        print('Quitting')
        break
    num1 = float(input('Enter the first number: '))
    num2 = float(input('Enter the second number: '))
    if operation == '+':
        k = num1+num2
    elif operation == '-':
        k = num1-num2
    elif operation == '*':
        k = num1*num2
    elif operation == '/':
        if num2 == 0:
            print ('You can not divide a number by zero')
        else:
            k = num1/num2
    elif operation == '%':
        k = num1%num2
    else:
        print('Unexpected Operation')
        continue # this will skip printing the result
    print('The result of', num1, operation, num2,'is: ', k)
It can be refactored to be more pythonic or logic to be better (e.g. - you don't need to ask for numbers if operation is unexpected, move that part forward)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,598 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  Invalid syntax error - need help fixing calgk01 3 3,266 Feb-23-2021, 08:41 PM
Last Post: nilamo
  Fixing a code error Twoshawns 5 2,744 May-14-2020, 11:56 AM
Last Post: Twoshawns
  Help Fixing Code kianwalters05 5 3,948 May-12-2020, 12:13 PM
Last Post: kianwalters05
  Fixing "PermissionError: [Errno 13] Permission denied" puredata 17 72,425 Mar-09-2020, 03:20 PM
Last Post: syssy
  Fixing a problem with file.io ThickTac 2 2,618 Mar-13-2019, 10:13 PM
Last Post: ThickTac
  Fixing indentation issues. MuntyScruntfundle 9 4,193 Feb-02-2019, 05:55 PM
Last Post: snippsat
  Can anyone Please help on fixing this python code to connect and save things on sqlit Roscoes 2 2,894 Mar-06-2018, 04:48 AM
Last Post: Roscoes

Forum Jump:

User Panel Messages

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