Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
eval not working in code
#1
Hello,

I am new to Python and am working on a project to build a calculator. I am using Pycharm to build this. Eval does not work in the code for the calculator, however, if I do a simple eval statement like x = 1 and print(eval('x + 1')) it works. Any idea why? Here is code:
import re

print("Our Magic Calculator")
print("Type 'quit' to exit\n")

previous = 0
run = True

def performMath():
    global run
    global previous
    equation = input("Enter equation:")
    if equation == 'quit':
        run = False
    else:
        previous = eval(equation)
        print(equation)

while run:
    performMath()

Figured it out...I had print(equation) not print(previous) DUH!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 954 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 876 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,001 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,068 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,919 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 1,979 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  How to avoid exec(), globals(), locals(), eval() paul18fr 10 5,007 Apr-21-2021, 05:53 PM
Last Post: snippsat
  code is not working , can anybody help? RandomPerson69 4 2,895 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,660 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 10,086 Dec-21-2020, 10:58 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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