-2

I am getting an problem that usr_inp is a string.

 class Student:

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

    def greet(self):
        return "Hello " + str(self.name)


justin = Student("Justin")
eva = Student("Eva")

usr_inp = input("Enter your name : ")

if usr_inp == "justin" or "eva":
    print(usr_inp.greet())

So I know how to fix the problem here, there are other several ways but I want a way to change the usr_inp the variable used above. Help me

3 Answers3

1

Your if statement is wrong and usr_inp is a string which doesn't implement greet() method while justin and eva are instance of Student class.

0

Change this:

justin = Student("Justin")
eva = Student("Eva")

usr_inp = input("Enter your name : ")

if usr_inp == "justin" or "eva":
    print(usr_inp.greet())

to this:

usr_inp = input("Enter your name : ")

if usr_inp == "Justin" or usr_inp == "Eva":
    print(Student(usr_inp).greet())
Foreen
  • 369
  • 2
  • 17
0

Those two lines are wrong,

if usr_inp == "justin" or "Eva":
    print(usr_inp.greet())

It should be like this,

usr_inp = input("Enter your name : ")

if usr_inp == "justin" or usr_inp == "eva":
    print(Student(usr_inp).greet())
Avishka Dambawinna
  • 1,180
  • 1
  • 13
  • 29