~ User to view films: Storing their film views in a file


Sample Project - Netflix type system

*Teachers with subscriptions will have access to all worked solutions and python code. This sample project is based on OCR GCSE NEA Task 1

CHALLENGE: 

A user needs to view films. Present the user with a list and allow them to select (by index number) the film they wish to watch. You may chose to do this in a different way. Once they have viewed a film, pop a message up on the screen to tell them that they have viewed that particular film (genre and title). In the next bit we will look at storing these 'views' to file for intelligent profiling.

Example file called films.txt

Note the field "likes" - this is where the likes will be stored for each film

0,Genre, Title, Rating, Likes
1,Sci-Fi,Out of the Silent Planet, PG, 0
2,Sci-Fi,Solaris, PG,0
3,Sci-Fi,Star Trek, PG, 0
4,Sci-Fi,Cosmos, PG, 0
5,Drama, The English Patient, 15, 0
6,Drama, Benhur, PG, 0
7,Drama, The Pursuit of Happiness, 12, 0
8,Drama, The Thin Red Line, 18, 0
9,Romance, When Harry met Sally, 12, 0
10,Romance, You've got mail, 12, 0
11,Romance, Last Tango in Paris, 18, 0
12,Romance, Casablanca, 12, 0

SUGGESTED SOLUTION / CODE: 

Note: See if you can do better (and you should be able to). The solutions provided are basic, and allow for discussion on possible alternative methods and solutions.

#Netflix type system demo - FakeFlix
import csv
import sys
import string #for use in the secure password and other parts of the program
#note in a later stage in the programming, we will be passing the parameter "username" to all  functions so as to refer to the unique username

def main():
   menu()

def menu():
    print("************Welcome to FakeFlix Demo**************")
    print()

    choice = input("""
                      A: Please Register
                      B: Login
                      Q: Logout

                      Please enter your choice: """)

    if choice == "A" or choice =="a":
        register()
    elif choice == "B" or choice =="b":
        login()
    elif choice=="Q" or choice=="q":
        sys.exit
    else:
        print("You must only select either A or B")
        print("Please try again")
        menu()

def long_enough(pw):
    'Password must be at least 6 characters'
    return len(pw) >= 6

def short_enough(pw):
    'Password cannot be more than 12 characters'
    return len(pw) <= 12

def has_lowercase(pw):
    'Password must contain a lowercase letter'
    return len(set(string.ascii_lowercase).intersection(pw)) > 0

def has_uppercase(pw):
    'Password must contain an uppercase letter'
    return len(set(string.ascii_uppercase).intersection(pw)) > 0

def has_numeric(pw):
    'Password must contain a digit'
    return len(set(string.digits).intersection(pw)) > 0

def has_special(pw):
    'Password must contain a special character'
    return len(set(string.punctuation).intersection(pw)) > 0

def test_password(pw, tests=[long_enough, short_enough, has_lowercase, has_uppercase, has_numeric, has_special]):
    for test in tests:
        if not test(pw):
            print(test.__doc__)
            return False
    return True


def register():
  
    #user is prompted to input all the required fields
    print("Enter first name")
    global firstname
    firstname=input()
    print("Enter surname")
    global surname
    surname=input()
    print("Enter Date of Birth Format: dd/mm/yy")
    global dob
    dob=input()
    print("Enter first line of address")
    global firstlineaddress
    firstlineaddress=input()
    print("Enter Postcode")
    global postcode
    postcode=input()
    print("Enter Gender")
    global gender
    gender=input()
    print("Enter main genre of interest")
    global interest
    interest=input()
    print("Enter email address")
    global email
    email=input()
    substring=dob[-4:] #this sets the date of birth (last four characters that is the year) to substring
    print("Your unique username is", firstname+surname+substring)
    global username
    username=firstname+surname+substring
   #secure password checker
    passwordchecker()
    
                   
#note in a later stage in the programming, we will be passing the parameter "username" to this and other functions
def passwordchecker():
   password=input("Please enter a password - must be secure and meet our format requirements")
   if test_password(password):
       with open('fakeflixfile.txt','a') as fakeflixfile:
        fakeflixfileWriter=csv.writer(fakeflixfile)
        fakeflixfileWriter.writerow([username,password,firstname,surname,dob,firstlineaddress,postcode,gender,interest,email])
        print("Record has been written to file")
        fakeflixfile.close()
        menu()
   else:
         passwordchecker()
         
    
def login():
   #set a variable (boolean type) to true if the user is NOT logged on
   notloggedin="true"
   #while the user is not logged on (i.e. while the login credentials provided do not work ...)
   while notloggedin=="true":
      print("***WELCOME - PLEASE LOGIN")
   
   #open the file we are reading from
      with open("fakeflixfile.txt",'r') as fakeflixfile:
     #prompt the user to enter their login details
         username=input("Enter username:-")
         password=input("Enter password:-")
      #call upon our reader (this allows us to work with our file)
         fakeflixfileReader=csv.reader(fakeflixfile)
      #for each row that is read by the Reader
         for row in fakeflixfileReader:
            for field in row:
            
            #search for the required matches in user entry against what is stored in the file
               if field==username and row[1]==password:
                  print("Granted")
                  displayfilms()
                  notloggedin="false"
                  
              
         
def displayfilms():
   print("*******************WELCOME to FAKEFLIX**************************")
   print("~What would you like to do?~")
   choice = input("""
                      W: Watch a film
                      V: View your Recommendations
                      K: Search by Title
                      R: Search by Rating 
                      L: View your "LIKED" list
                      Q: Quit FakeFlix

                      Please enter your choice: """)

   if choice == "W" or choice =="w":
       watchfilms()
   elif choice == "V" or choice =="v":
        viewrecs()
   elif choice=="T" or choice=="t":
       searchbykeyword()
   elif choice=="R" or choice=="r":
       searchbyrating()
   elif choice=="L" or choice=="l":
       viewliked()
   elif choice=="Q" or choice=="q":
           sys.exit
   else:
        print("You must only select from the given options")
        print("Please try again")
        displayfilms()


def watchfilms():
   #Open the file for reading
   filmsfile=open("films.txt","r", encoding="utf8")
   #Create a list called displayfilms into which all the file lines are read into....
   displayfilmslist=filmsfile.read()
   #print the list (that now has the film details in it)
   print(displayfilmslist)
   filmsfile.close()
   print("~What would you like to do?~")
   choice = input("""
                      Select a number to View a Film!
                                         or
                      F: Return to the FakeFlix Menu
                      Q: Quit FakeFlix

                      Please enter your choice: """)

   if choice == "F" or choice =="f":
       displayfilms()
   elif choice == "Q" or choice =="q":
       sys.exit
   elif choice=="1":
       viewfilmfunction(1)
   elif choice=="2": 
       viewfilmfunction(2)
   elif choice=="3": 
       viewfilmfunction(3)
   elif choice=="4": 
       viewfilmfunction(4)
   elif choice=="5": 
       viewfilmfunction(5)
   elif choice=="6": 
       viewfilmfunction(6)
   elif choice=="7": 
       viewfilmfunction(7)
   else:
        print("You must only select from the given options")
        print("Please try again")
        displayfilms()

def viewfilmfunction(x):
   #open the file as student file (variable)
   print("You are about to view Film:", x, "Enter the selection ID number of the film again to confirm viewing")
   with open("films.txt","r") as filmsfile:
        #prompt the user to enter the ID number they require
        idnumber=input("Enter the ID number you require:")
        #call upon our reader (this allows us to work with our file)
        filmsfileReader=csv.reader(filmsfile)
        #for each row that is read by the Reader    
        for row in filmsfileReader:
            #and for each field in that row (this does it automatically for us)
            for field in row:
                #if the field is equal to the id number that is being searched for
                if field ==idnumber:
                    #print the row fields (genre and title) corresponding to that ID number
                   #create a list which contains the relevant fields in the row.
                  viewedlist=[row[1],row[2]]
                  print("You have viewed:", viewedlist)

         
   
      
#the program is initiated, so to speak, here
main()


Analyse

Write your own summary of the problem. What are your objectives? List the success criteria

Design Tools

Designing something or writing out some pseudocode before you actually write code is always a good idea! Get in to the habit of doing so! You can draw your flowchart here and screenshot it.

Try it yourself

Testing Table

You may want to read a little about Testing first. A teacher may go through some examples with you. Feel free to fill in the test table here, and screenshot it in to your powerpoint. Testing is absolutely essential once you have created a program!
Test No. Description Test Data(input) Expected Outcome Actual Outcome Further Action?
1
2
3
4
5
Coming soon!