Hi, I'm trying to learn OOP, and I've run into a problem. I am supposed to have the user create a user name, and then I'm supposed to import a list of movies for the user to rate. I created a class called Movie. The function within is supposed to create a list of all the movies in the file. However, when I try to use the list in the rateMovie() function, the list is empty. I highlighted what IDLE says is my problematic bit of code. Any ideas of what I'm doing wrong?
#! /usr/bin/python
__author__="***"
__date__ ="***"
class User(object):
def __init__(self):
self.userName= ''
self.ratings= '' #empty strings to store user inputted ratings and userName
def enterName(self):
import string
self.userName=raw_input('Enter a new user name.\n')
try:
userFile=open('output.txt', 'r+a')
users=map(string.strip, userFile.readlines())
n=0
while n < len(users):
if self.userName==users[n]:
print "I'm sorry that user name has been taken."
return self.enterName()
else:
n+=1
except:
userFile=open('output.txt', 'w')
userFile.close()
def rateMovie(self):
try:
userFile=open('output.txt', 'r')
except:
print "File output.txt could not be found."
movie=Movie().getMovies().movieList
n=0
print 'Hello ' + self.userName +'. Welcome to MyFlix. To create user suggestions, MyFlix would like you to rate the following movies using our 5-star system.\n Enter 5 for "loved it"\n 4 for "liked it"\n 3 for "okay"\n 2 for "didn\'t like it"\n 1 for "hated it"\n or 0 for "haven\'t seen it"'
print movie
for line in movie:
userInput=raw_input('Enter a rating 0-5 for ' + movie[n] + '\n')
if 0<=userInput<=5:
self.ratings.append(userInput)
n+=1
else:
print "Invalid input. Try again."
return
userFile.close()
def outputUserData(self):
userFile=open('output.txt', 'a')
userFile.write(self.userName + self.ratings + '\n')
userFile.close()
class Movie(object):
def __init__(self):
self.movieList= []
def getMovies(self):
movieFile=open('moviestorate.txt', 'r')
for line in movieFile:
self.movieList.append(line)
movieFile.close()
def recommendMovie(self):
pass
#Code to recommend movie to user
def myFlix():
newUser=User()
newUser.enterName()
newUser.rateMovie()
newUser.outputUserData()
myFlix()