0
#!/usr/bin/python

Import time
Import Script1
Import Script2
Import Script3

Options = {'Script1.py', 'Script2.py', 'Script3.py'}
no_of_options = 3

choice = int( time.time() ) %no_of_options

if choice == 0:

            python (Script1.py)

if choice ==1:

           python (Script2.py)

if choice == 2:

           python (Script3.py)

I want to randomize the script chosen. But I've noticed that the first script is always executed no matter what. Also, if I write 'import Script2.py' first, then Script2 gets executed first no matter what. Please help me fix this.

Please offer some help. I have written three modules as separate .py files and a different one needs to be executed randomly. Tell me how to execute a different module each time depending on the value of choice.

Edit : Here is what I want to do. There are three different patterns available. I have written a separate Python program to print each of these patterns. One of these three patterns must randomly be executed. What I have tried to do is get a random number and then accordingly execute one of these three scripts to display the corresponding pattern.

Please correct the syntax or suggest a better way to do this.

Saikat
  • 111
  • 5
  • You will want to not import everything at the top. try `if choice==1: import Script1` take a look at docs for more on how `import` works – Will Sep 14 '16 at 13:43
  • Please format your code properly. Is it real, integral, working code ? Because it doesn't look like so. – Guillaume Sep 14 '16 at 13:44
  • 3
    Also note that `python (Script1.py)` isn't even vaguely valid syntax. – Daniel Roseman Sep 14 '16 at 13:44
  • 1
    Adding to @Will, it might make sense to have the `import`s always take the form `import Script1 as Script`, if you intend to use common names from different modules later; this way, you don't have to constantly recheck `choice`, since `Script` will be the correct module. – ShadowRanger Sep 14 '16 at 14:02
  • @Guillaume I changed the names of the scripts, but it is. – Saikat Sep 14 '16 at 14:34
  • @DanielRoseman This is the first project I did in Python. Please tell me correct syntax. – Saikat Sep 14 '16 at 14:35
  • What's in your Script1.py Script2.py Script3.py files then ? And also, `import` statement is all lowercase – Guillaume Sep 14 '16 at 14:36
  • @Guillaume A different shape is drawn using Turtle module, depending on which script is called. – Saikat Sep 14 '16 at 14:37
  • @Will Please tell me the correct code. – Saikat Sep 14 '16 at 14:38
  • Try running a script with the following line only and let me know what happens: `import Script1` – Guillaume Sep 14 '16 at 14:54
  • @Guillaume I didn't get you. Should I write this inside the if statement or beginning? – Saikat Sep 14 '16 at 15:00
  • 1
    Comments are too restricted. When you import your Script1, Script2, etc. at the beginning of your file, python actual *executes* their contents. your code to choose a random script is meaningless. See @DanielRoseman comments for an accurate explanation about that. Can't post actual response now that the question is flagged as duplicate. – Guillaume Sep 14 '16 at 15:05
  • @Guillaume I am used to writing in C. I thought it was like including libraries in a C program. We just include it for later use. Is it different ? Please expand on why you think it is meaningless, and give suggestions on how I can make it meaningful. Obviously, time.time() would give a new value each time. My thinking was to use this to call a different script by taking the remainder with 3. Tell me how I can implement this. – Saikat Sep 15 '16 at 06:32
  • Very difficult to accurately explain without knowing what is in your Script*.py files,and what these mysterious `python()` call are (as mentionned by @DanielRoseman it is not standard and not even syntaxically valid). – Guillaume Sep 16 '16 at 08:04
  • The duplicate question shows how you can write modules that can be included without immediately being executed. Please read it. – Daniel Roseman Sep 16 '16 at 08:10
  • @DanielRoseman Please help me. The other question seems a bit different from mine. – Saikat Oct 16 '16 at 03:58

0 Answers0