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()

getMovies() does not return the object instance.
You can try to break out into the code below.

movie=Movie()
movie.getMovies()
movielist = movie.movieList

You are going to have trouble here, too:

userInput=raw_input('Enter a rating 0-5 for ' + movie[n] + '\n')
      if 0<=userInput<=5:

The return from raw_input is a string, not an integer. You need the line to be userInput=int(raw_input('Enter a rating 0-5 for ' + movie[n] + '\n'))

P.S. I don't know how you managed to get indented text for your code without line numbers. Next time, please use the (CODE) button to place (CODE) and (/CODE) tags around your code, because that not only protects indentation, but adds keyword highlighting and line numbers.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thanks! That really helped. I didn't notice the code button before, so I just typed it in around my code (possibly incorrectly), perhaps that's why there were no number lines. I'll certainly keep it in mind next time.

This question has already been answered. Start a new discussion instead.