0

So I'm writing a little bit of code as a fun project that will randomly generate a subject for me to study each day. But once a subject has appeared once I don't want it to appear for the rest of the week. To do this I'm using a list. Basically, when it picks the subject it adds it to the list and the next time it checks to see if it's already on the list, and if so, I want it to return to the random number generator. How do I do this? Here's my code.

import random
import time
import datetime

#Subject veto list
x=[]

# Messages to instil, um, enthusiasm.
if datetime.date.today().strftime("%A") == "Monday":
    response = input("Here we go again. Are you ready? ")
elif datetime.date.today().strftime("%A") == "Tuesday" or "Wednesday" or "Thursday":
    response = input("Are you ready? ")
elif datetime.date.today().strftime("%A") == "Friday":
    response = input("Half day! Are you ready? ")
elif datetime.date.today().strftime("%A") == "Saturday" or "Sunday":
    response = input("It's the weekend! Are you ready? ")

# Random picking of subject to study. Also adds sbject to veto list for rest of week.
if response == "Yes":
        subject = random.randint(1, 7)
        print("Today you are studying...")
        time.sleep(3)
        if subject == (1):
            "Englsh" in x
            print("English")
            x.extend([English])
        elif subject == (2):
            "Irish" in x
            print("Irish")
            x.extend([Irish])
        elif subject == (3):
            "Maths" in x
            print("Maths")
            x.extend([Maths])
        elif subject == (4):
            "French" in x
            print("French")
            x.extend([French])
        elif subject == (5):
            "Physics" in x
            print("Physics")
            x.extend([Physics])
        elif subject == (6):
            "Chemistry" in x
            print("Chemistry")
            x.extend([Chemistry])
        elif subject == (7):
            "History" in x
            print("History")
            x.extend([History])
Ferex
  • 65
  • 10
  • The problem is that the veto list won't persist. Meaning that each time you run the program, the list will be empty so all options will always be available. If you want to prevent the same option for some period of time you need to persist the information about what options you've picked recently by storing it to a database or file. – Chad S. Sep 01 '15 at 20:35
  • what if i were to leave the program running continuously? – Ferex Sep 01 '15 at 20:37
  • That would work as long as your computer didn't restart, etc. Really though you will want to persist the information. – Chad S. Sep 01 '15 at 20:38
  • As @polpak stated you need to store that list to some kind of file or some "database". So the program reads from there. And than compare which subject has already been random picked. Reading and writing to files you can find here:http://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file-in-python – Hybr1d Sep 01 '15 at 20:38
  • 1
    Why do you have, e.g., `"History" in x`? You're wasting cycles doing comparisons and not storing the results. Also, `"something" == "x" or "y"` isn't doing what you think it is. – Two-Bit Alchemist Sep 01 '15 at 20:39
  • okay, i am working off a raspberry pi so it wouldnt consume too much power. But could you help me with writing it to a file? At least answer my original question and i can take it from there – Ferex Sep 01 '15 at 20:39
  • @Two-BitAlchemist The ""History" in x" is the start of a comparison, but i wasnt sure where to go – Ferex Sep 01 '15 at 20:41
  • Rewrote your code to fix all peripheral problems: http://pastebin.com/SiGSdhuC. Note that I did not address the main problem; it still does the same thing as your code. The way you would address the main problem though is by storing the subjects in a file using json. – pzp Sep 01 '15 at 20:52
  • do you have a link as to where i can learn how to do that? – Ferex Sep 01 '15 at 21:04
  • @Ferex, you don't need to store state across runs in this case; see my answer. – Cyphase Sep 01 '15 at 21:12

3 Answers3

2

This function will let you choose a random thing from a list based on the weekday, without repeating* and without having to store anything between runs. The only potential issue would be if Python's PRNG was changed mid-week :P.

import datetime
import itertools
import random

def random_choice_per_date(iterable, date=None):
    choices = list(itertools.islice(iterable, 7))
    date = date or datetime.date.today()

    year, week, weekday = date.isocalendar()

    rand = random.Random((year, week))  # Seed PRNG with (year, week)
    rand.shuffle(choices)

    index = weekday % len(choices)

    return choices[index]

This is generalizable beyond dates, but I didn't want to complicate the code.

* It will repeat if the iterable has less than seven items. It also only uses the first seven items at most.

Cyphase
  • 11,502
  • 2
  • 31
  • 32
1

I hope below snippet will help you.
It shows you how you can get a random element from a list.
After getting that element, the list takes out that element from the list.

import random

def get_random_and_pop(lst):
    value = random.choice(lst)
    lst = list(set(lst) - set([value]))
    return value,lst

lst = ['english','math','science','computer science','history']
while (len(lst)):
    v,lst = get_random_and_pop(lst)
    print v,lst
    if (len(lst) == 0):
        lst = ['english','math','science','computer science','history']
taesu
  • 4,482
  • 4
  • 23
  • 41
-1

If you put the selection algorithm in a function, and iterate that function until the returned value doesn't appear in the 'banned' list, then print the resultant non-banned value, you'd be done!

Don't actually do it that way, it would be a terrible method. simply remove the item from the options list when it's put on the banned list.. then move it back after a week.

Alea Kootz
  • 913
  • 4
  • 11