I keep getting an error when i run the code, I am trying to have class method wh
ID: 3579717 • Letter: I
Question
I keep getting an error when i run the code, I am trying to have class method where the name of the user is put into a dictionary. Can someone please fix the error:
Data read from file:
Traceback (most recent call last):
File "Farhat_project.py", line 132, in
main()
File "Farhat_project.py", line 113, in main
user.makedictionary(name)
AttributeError: User instance has no attribute 'makedictionary'
# -*- coding: utf-8 -*-
class MathZombie:
left = 0
right = 0
def __init__(self, a, b):
self.left = a
self.right = b
def add(self):
return self.left + self.right
def multiply(self):
return self.left * self.right
def isEven(self):
if (self.left % 2 == 0 and self.right % 2 == 0):
return True
else:
return False
def drawWinningPattern(self):
for i in range(4):
for j in range(1, i):
print "########### ",
print('[31m' + 'YOU WON')#makes color red
print('[30m')#resets color back to white
def drawLoserPattern(self):
for i in range(2):
for j in range(1,i):
print "~~~~~~~~~",
print " ", " "," "
#prints fail if you fail
#class for the strory to continue after you exit the barn; this is the user class
class User:
username = ''
def __init__(self, name):
username = name
def encounterBigZombie(self):
print "'you exit the barn and walk 1 mile out when suddenly...'"
print "BIG MATH ZOMBIE!!"
print "'you can't run, the big math zombie is blocking your path, You MUST FIGHT!'"
print "You would need to any two integers. Enter first integer now: "
left = input()
right = input("Enter second integer: ")
bigZombie = MathZombie(left, right)
result = input("In order to kill zombie give correct answer. What is: "+str(left)+" * "+str(right)+": ")
if (result == bigZombie.multiply()):
print "Good job. You won!"
bigZombie.drawWinningPattern()
else:
print "Wrong!!!! The correct answer is: " + str(bigZombie.multiply())
print "You lost."
bigZombie.drawLoserPattern()
def choiceOnePath(self):
print "You and your friend, Steven, are camping at a deserted barn. What would you like to do?"
print "Enter '1' to talk your friend, Steven?"
print "Enter '2' to exit the barn?"
choiceOnePath = input("Enter your choice: ")
while (choiceOnePath != 1 and choiceOnePath != 2 and choiceOnePath != 420):
choiceOnePath = input("That's Not an Option! Press either '1' or '2'.")
if (choiceOnePath == 1):
print " !!!----------------------Chapter One: Escape----------------------!!!"
print " You: Hey Steven, how's your broken leg healing up?"
print "Steven: Not so good. I need you to go and get me pain medication from the CVC 3 miles down, can you do that for me?"
print "You: Sure man, I'll try to be back before dark."
print "'you exit the barn with a pocket knife, a gun with 3 bullets, and your watch"
elif (choiceOnePath == 2):
print " !!!----------------------Chapter One: Escape----------------------!!!"
print " You: Steven Im going out for a bit, do you need anything?"
print "Steven: Ya, if you can get me some meds to ease up the pain for my broken leg..."
print "You: Sure man, I'll try to be back before dark with those meds, see you soon."
print "'you exit the barn with a pocket knife, a gun with 3 bullets, and your watch"
elif (choiceOnePath == 420):
print "Cheat code activated. You won the game my friend!!"
fakeZombie = MathZombie(0, 0)
fakeZombie.drawWinningPattern()
return choiceOnePath
def writeToFile(self, name):
f = open("sasha.txt", "w")
print "Writing to the file: "+ name
f.write(name)
f.close()
def readFromFile(self):
f = open("sasha.txt", "r")
print "Data read from file:"
f.readline()
f.close()
def makedictionary(self, name):
dictionary = {}
data = line.split(" ")
key = data[0]
value = data [1]
print "User First and Last Name Stored in dictionary"
dictionar[key] = value
def main():
print('[31m' + 'Welcome Brave Soul')
print('[37m')
name = raw_input("You are one of the few to survive a zombie apocolyps, what is your first & last name?")
user = User(name)
user.writeToFile(name)
user.readFromFile()
user.makedictionary(name)
choiceOnePath = user.choiceOnePath()
if (choiceOnePath == 1):
print "*********************************************** "
print "Bad choice. Something bad is about to happen."
user.encounterBigZombie()
elif (choiceOnePath == 2):
print "*********************************************** "
print "Good choice. All you have to do now is to enter to even numbers. If you make a mistake you will lose"
a = input("Enter first number: ")
b = input("Enter second number: ")
smallZombie = MathZombie(a, b)
if (smallZombie.isEven()):
print "Correct answer. You won!"
smallZombie.drawWinningPattern()
else:
print "You are so dumb! You lost. :("
smallZombie.drawLoserPattern()
main()
Explanation / Answer
# -*- coding: utf-8 -*-
class MathZombie:
left = 0
right = 0
def __init__(self, a, b):
self.left = a
self.right = b
def add(self):
return self.left + self.right
def multiply(self):
return self.left * self.right
def isEven(self):
if (self.left % 2 == 0 and self.right % 2 == 0):
return True
else:
return False
def drawWinningPattern(self):
for i in range(4):
for j in range(1, i):
print "########### ",
print('[31m' + 'YOU WON')#makes color red
print('[30m')#resets color back to white
def drawLoserPattern(self):
for i in range(2):
for j in range(1,i):
print "~~~~~~~~~",
print " ", " "," "
#prints fail if you fail
#class for the strory to continue after you exit the barn; this is the user class
class User:
username = ''
def __init__(self, name):
username = name
def encounterBigZombie(self):
print "'you exit the barn and walk 1 mile out when suddenly...'"
print "BIG MATH ZOMBIE!!"
print "'you can't run, the big math zombie is blocking your path, You MUST FIGHT!'"
print "You would need to any two integers. Enter first integer now: "
left = input()
right = input("Enter second integer: ")
bigZombie = MathZombie(left, right)
result = input("In order to kill zombie give correct answer. What is: "+str(left)+" * "+str(right)+": ")
if (result == bigZombie.multiply()):
print "Good job. You won!"
bigZombie.drawWinningPattern()
else:
print "Wrong!!!! The correct answer is: " + str(bigZombie.multiply())
print "You lost."
bigZombie.drawLoserPattern()
def choiceOnePath(self):
print "You and your friend, Steven, are camping at a deserted barn. What would you like to do?"
print "Enter '1' to talk your friend, Steven?"
print "Enter '2' to exit the barn?"
choiceOnePath = input("Enter your choice: ")
while (choiceOnePath != 1 and choiceOnePath != 2 and choiceOnePath != 420):
choiceOnePath = input("That's Not an Option! Press either '1' or '2'.")
if (choiceOnePath == 1):
print " !!!----------------------Chapter One: Escape----------------------!!!"
print " You: Hey Steven, how's your broken leg healing up?"
print "Steven: Not so good. I need you to go and get me pain medication from the CVC 3 miles down, can you do that for me?"
print "You: Sure man, I'll try to be back before dark."
print "'you exit the barn with a pocket knife, a gun with 3 bullets, and your watch"
elif (choiceOnePath == 2):
print " !!!----------------------Chapter One: Escape----------------------!!!"
print " You: Steven Im going out for a bit, do you need anything?"
print "Steven: Ya, if you can get me some meds to ease up the pain for my broken leg..."
print "You: Sure man, I'll try to be back before dark with those meds, see you soon."
print "'you exit the barn with a pocket knife, a gun with 3 bullets, and your watch"
elif (choiceOnePath == 420):
print "Cheat code activated. You won the game my friend!!"
fakeZombie = MathZombie(0, 0)
fakeZombie.drawWinningPattern()
return choiceOnePath
def writeToFile(self, name):
f = open("sasha.txt", "w")
print "Writing to the file: "+ name
f.write(name)
f.close()
def readFromFile(self):
f = open("sasha.txt", "r")
print "Data read from file:"
f.readline()
f.close()
def makedictionary(self, name):
dictionary = {}
data = name.split(" ")
key = data[0]
value = data[1]
print "User First and Last Name Stored in dictionary"
dictionary[key] = value
def main():
print('[31m' + 'Welcome Brave Soul')
print('[37m')
name = raw_input("You are one of the few to survive a zombie apocolyps, what is your first & last name?")
user = User(name)
user.writeToFile(name)
user.readFromFile()
user.makedictionary(name)
choiceOnePath = user.choiceOnePath()
if (choiceOnePath == 1):
print "*********************************************** "
print "Bad choice. Something bad is about to happen."
user.encounterBigZombie()
elif (choiceOnePath == 2):
print "*********************************************** "
print "Good choice. All you have to do now is to enter to even numbers. If you make a mistake you will lose"
a = input("Enter first number: ")
b = input("Enter second number: ")
smallZombie = MathZombie(a, b)
if (smallZombie.isEven()):
print "Correct answer. You won!"
smallZombie.drawWinningPattern()
else:
print "You are so dumb! You lost. :("
smallZombie.drawLoserPattern()
main()