I have used from_input
function to store user input. I tried to visualize the below code on pythontutor and what I can understand from it, input values are stored in __init__
function after from_input
function returns.
Now the problem begin when random_fortune
is called and I am trying to print the name already stored. I have tried self.name
and name
but it is giving error.
import random
class Fortune:
def __init__(self, name, color, age):
self.name = name
self.color = color
self.age = age
@classmethod
def from_input(self):
name = input('Name: ')
color = input('Color: ')
age = int(input('Age: '))
return self(name,color,age)
@staticmethod
def random_fortune():
print("hi" + name)
random_no = random.randint(1, 9)
if random_no == 1:
print("you are in luck today")
else:
print("Bad Luck mate")
user = Fortune.from_input()
Fortune.random_fortune()