-2

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()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    If random_fortune needs access to the name, why is it a static method? Make it an instance method, and call it on user. – jonrsharpe Mar 14 '20 at 19:16
  • 2
    You're doing a few things that don't make sense. If the method is static or a class method, you can't use `self`. You also presumably want to assign to `self.color` and the like instead of creating a new `color` variable. `self(...)` is also unclear. – Carcigenicate Mar 14 '20 at 19:17
  • 1
    Can you clarify what you're trying to do? _but it is giving error._ Then please share the entire error message. – AMC Mar 14 '20 at 19:24

3 Answers3

0

You've created a Fortune object but didn't use it. Also your random_fortune is not a static method because u want to use the class attribute "name" in it. So this should work:

import random class Fortune: 
    def __init__(self, name, color, age): 
        self.name = name 
        self.color = color
        self.age = age

   @staticmethod
   def from_input(self): 
       name = input('Name: ') 
       color = input('Color: ') 
       age = int(input('Age: ')) 
       return Fortune(name,color,age)

   def random_fortune(self): 
       print("hi" + self.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()
user.random_fortune()
Nico
  • 69
  • 5
0
user = Fortune.from_input()

Python will attempt to call a static function in the Fortune class named from_input, you have decorated from_input with @classmethod and it will therefore not be accessible unless you create an object of the Fortune class.

You create an object by calling the constructor of that object. In this case

user = Fortune()

However, it seems like you wish to create an object using input, you can do this by decorating the from_input function with @staticmethod and changing the return statement to

return Fortune(name, color, age)

You should also remove the @staticmethod decorator from random_fortune and replace Fortune.random_fortune() with user.random_fortune()

This is what it should look like

import random
class Fortune:
    name = ""
    color = ""
    age = 0

    def  __init__(self, name, color, age):
        self.name = name
        self.color = color
        self.age = age

    @staticmethod
    def from_input(self):
        name = input('Name: ')
        color = input('Color: ')
        age = int(input('Age: '))
        return Fortune(name,color,age)

    @classmethod
    def random_fortune(self):
        print("Hi " + self.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()
user.random_fortune()
AllTheHotkeys
  • 81
  • 1
  • 7
-1

I think you are looking for something like this.
Basically you pass as a parameter the newly created user to the static method.
Since it's a static method you can't access the class variable using self, and as others pointed out, you need to create an object for this method to work.

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(user):
        print("Hi " + user.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(user)

When run with the following inputs it prints.

Name: Bill
Color: green
Age: 35
Hi Bill
Bad Luck mate
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • Any particular reason for the downvote? This code works as expected and respects the `@staticmethod` decoration as requested by the OP. – solid.py Mar 14 '20 at 19:53
  • Thanks @solid.py, this worked. I really trying to understand how this works, I guess I jumped few topics during practice. – code.rookie Mar 14 '20 at 20:54
  • @RahulGoyal To further elaborate, the core difference is that, a static method cannot access the class object and its fields directly, you need to pass it as an input should you wish to access it. As to why this works, you can see in more detail here: https://stackoverflow.com/questions/136097/difference-between-staticmethod-and-classmethod – solid.py Mar 14 '20 at 20:58