Python Forum
Entering an expression with "input" command
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Entering an expression with "input" command
#1
I want to write a program that lets the user provide an expression "on the fly" as in
expression = input ("Enter the expression")
for x in range (-10,10):
y = evaluate(expression(x))
print (y)

The idea is that I want to evaluate a user-supplied expression without having to change the source code of my program. Does Python have such a function or library? Maybe something n sympy?

Thanks - John Mycroft
Reply
#2
I don't for such a module but using evaluate is a "bit" dangerous. What if someones input is this: input subprocess ; subprocess.call(['rm', '-rf', '/home/$USER']) ?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Don't actually try wavic's example. It will erase disk!
He only meant to show dangerous example.
evaluate will actually execute code typed in!
Reply
#4
Quote: Maybe something n sympy?
yes sympy is a safe way to handle expressions
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,073 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  problem in using input command akbarza 4 1,103 Oct-19-2023, 03:27 PM
Last Post: popejose
  problem in entering address of a file in input akbarza 0 648 Oct-18-2023, 08:16 AM
Last Post: akbarza
  How to input & output parameters from command line argument shantanu97 1 2,554 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  syntaxerror when entering a constructor MaartenRo 2 1,979 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  Taking Multiple Command Line Argument Input bwdu 6 4,022 Mar-29-2020, 05:52 PM
Last Post: buran
  Pass results of expression to another expression cmdr_eggplant 2 2,279 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  command line input (arg parse) and data exchange Simba 7 4,322 Dec-06-2019, 11:58 PM
Last Post: Simba
  How to Stop Sentinel Value from Entering Final Output ZQ12 3 3,224 Nov-11-2019, 07:25 AM
Last Post: perfringo
  Error when entering letter/character instead of number/integer helplessnoobb 2 7,056 Jun-22-2019, 07:15 AM
Last Post: ThomasL

Forum Jump:

User Panel Messages

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