Python Forum
Clear button in calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Clear button in calculator
#1
Hey! I have just started a python course and Im having a hard time to put a clear button on my calculator! Thanks for helping!
This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import re
 
print("First calculator")
print("Type 'quit' to exit\n")
 
previous = 0
run  = True
 
def performMath():
    global run
    global previous
    equation = ""
     
    if previous == 0:
        equation = input("Enter equation:")
    else:
        equation = input(str(previous))
 
    if equation == 'quit':
        print("Goodbye, human!")
        run = False
    else:
        equation = re.sub('[a-zA-Z:" "]', '', equation)
 
        if previous == 0:
            previous = eval(equation)
        else:
            previous = eval(str(previous) + equation)
 
        print("You typed", previous)
 
while run:
    performMath()
Reply
#2
Look at your post. Without the indentation it is impossible to figure out what is wrong. Please repost using the python /python marks.
Reply
#3
You already know how to use functions, why not write functions to clear/close and call them from your if/else?

like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def main():
   *your calculator code*
   if something:
      clear()
   else:
      close()
 
def clear():
   print(" ")
   main()
 
def close():
   exit()
 
main()
Reply


Forum Jump:

User Panel Messages

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