-1

The sample code is below. I am trying to get the inputted value for the 'name' variable from the first method and use it to print a message using the second method (method2). I would appreciate any advice on how to do this or alternative ways to go about it.

  class User:

     method1(self):
        name = input("Enter name")

     method2(self):
        greeting = print("Hello", name)
frr012
  • 1
  • 1
  • 1
    Have you read any tutorials on OOP? They should all explain the concept of object attributes. – Barmar Jan 06 '20 at 21:04

2 Answers2

0

Please check What __init__ and self do on Python?

You should assign the input string to class variable

 method1(self):
        self.name = input("Enter name")

and then you can use it in other method.

method2(self):
        greeting = print("Hello", self.name)

Greetings!

Dawid Gacek
  • 544
  • 3
  • 19
0

#

class User():

def method1(self): 
    self.name = input('Enter your name:')

def method2(self): 
    print("Name is", self.name ) 

obj = User()

obj.method1()

obj.method2()

#

Also read this

https://www.geeksforgeeks.org/self-in-python-class/

GiGeNCo
  • 31
  • 1
  • 3