Posts: 56
Threads: 23
Joined: Jul 2021
May-16-2022, 09:17 AM
(This post was last modified: Jun-24-2022, 10:15 AM by AlphaInc.)
Hello everybody,
I wanted to set up a list of strings and print one randomly.
This was really easy to set up:
#Random Variables
myList = ["Example 1", "Example 1", "Example 1", "Example 1"]
random = random.choice(myList)
print(random) Now since this script (it's of course only an example, my actual script is more complex but I'm only focussing on the important part here) runs twice a day, I want the random choice to be the same if the day is still the same.
For example, if I run this script five times a day I always want it to be example 4 and on another day it should pick another random which stays for this day.
Is it possible to do this and share it with another script or do I need to create some kind of config which stores the daily random-pick ?
Posts: 7,312
Threads: 123
Joined: Sep 2016
(May-16-2022, 09:17 AM)AlphaInc Wrote: For example, if I run this script five times a day I always want it to be example 4 and on another day it should pick another random which stays for this day.
Is it possible to do this and share it with another script or do I need to create some kind of config which stores the daily random-pick ? Can store the random value and day in eg dictionary and save to json.
The do a check if same day and then print same value all day if True.
New day will store a new value and print same value until next day.
To give a example.
# rand_pick.py
import random
import pendulum
import json
def store(save_day):
with open('rand_today.json', 'w') as f:
json.dump(save_day, f)
myList = ["Example 1", "Example 2", "Example 3", "Example 4"]
pick_val = random.choice(myList)
today = pendulum.today().day
#today = 18
save_day = {}
save_day[pick_val] = today
if __name__ == '__main__':
store(save_day) # day_output.py
import rand_pick
import json
import pendulum
with open('rand_today.json') as f:
pick = json.load(f)
today = pendulum.today().day
#today = 18
if today in pick.values():
print(''.join(pick.keys()))
else:
# No match so a new day save new random vaule
rand_pick.store(rand_pick.save_day) Output: Example 4
Example 4
Example 4
Posts: 56
Threads: 23
Joined: Jul 2021
(May-16-2022, 12:23 PM)snippsat Wrote: (May-16-2022, 09:17 AM)AlphaInc Wrote: For example, if I run this script five times a day I always want it to be example 4 and on another day it should pick another random which stays for this day.
Is it possible to do this and share it with another script or do I need to create some kind of config which stores the daily random-pick ? Can store the random value and day in eg dictionary and save to json.
The do a check if same day and then print same value all day if True.
New day will store a new value and print same value until next day.
To give a example.
# rand_pick.py
import random
import pendulum
import json
def store(save_day):
with open('rand_today.json', 'w') as f:
json.dump(save_day, f)
myList = ["Example 1", "Example 2", "Example 3", "Example 4"]
pick_val = random.choice(myList)
today = pendulum.today().day
#today = 18
save_day = {}
save_day[pick_val] = today
if __name__ == '__main__':
store(save_day) # day_output.py
import rand_pick
import json
import pendulum
with open('rand_today.json') as f:
pick = json.load(f)
today = pendulum.today().day
#today = 18
if today in pick.values():
print(''.join(pick.keys()))
else:
# No match so a new day save new random vaule
rand_pick.store(rand_pick.save_day) Output: Example 4
Example 4
Example 4
Thank you :)
Last problem, when I run the script I get this error, any idea?:
Traceback (most recent call last):
File "helios_daySavingVar.py", line 4, in <module>
import rand_pick
ModuleNotFoundError: No module named 'rand_pick'
Posts: 7,312
Threads: 123
Joined: Sep 2016
Both file is same folder,and you run day_output.py (which import rand_pick.py).
Also install pendulum that i use.
pip install pendulum Example.
div_code\
|-- rand_pick.py
|-- day_output.py Output: G:\div_code
λ python day_output.py
Example 2
G:\div_code
λ python day_output.py
Example 2
G:\div_code
λ python day_output.py
Example 2
Posts: 56
Threads: 23
Joined: Jul 2021
(May-17-2022, 01:27 PM)snippsat Wrote: Both file is same folder,and you run day_output.py (which import rand_pick.py).
Also install pendulum that i use.
pip install pendulum Example.
div_code\
|-- rand_pick.py
|-- day_output.py Output: G:\div_code
λ python day_output.py
Example 2
G:\div_code
λ python day_output.py
Example 2
G:\div_code
λ python day_output.py
Example 2
Yes, both files are in the same directory and pendulum is installed but I still get this error
Posts: 7,312
Threads: 123
Joined: Sep 2016
May-17-2022, 01:41 PM
(This post was last modified: May-17-2022, 01:41 PM by snippsat.)
Try to exactly what show,also same name on the files.
Here you use a file helios_daySavingVar.py
Traceback (most recent call last):
File "helios_daySavingVar.py", line 4, in <module>
import rand_pick
ModuleNotFoundError: No module named 'rand_pick' Only when it work,then try to implement into your own code.
Posts: 56
Threads: 23
Joined: Jul 2021
(May-17-2022, 01:41 PM)snippsat Wrote: Try to exactly what show,also same name on the files.
Here you use a file helios_daySavingVar.py
Traceback (most recent call last):
File "helios_daySavingVar.py", line 4, in <module>
import rand_pick
ModuleNotFoundError: No module named 'rand_pick' Only when it work,then try to implement into your own code.
Ah shit... Sorry, I oversaw that ^^ Thank you, now it works :)
|