我遇到一个问题,即usr_inp是一个字符串。
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())
所以我知道如何在这里解决问题,还有其他几种方法,但是我想要一种方法来更改上面使用的usr_inp变量。 帮帮我
答案 0 :(得分:1)
您的if语句错误并且usr_inp
是string
,它没有实现greet()
方法,而justin
和eva
是{{1 }}类。
答案 1 :(得分:0)
更改此:
justin = Student("Justin")
eva = Student("Eva")
usr_inp = input("Enter your name : ")
if usr_inp == "justin" or "eva":
print(usr_inp.greet())
对此:
usr_inp = input("Enter your name : ")
if usr_inp == "Justin" or usr_inp == "Eva":
print(Student(usr_inp).greet())
答案 2 :(得分:0)
这两行是错误的,
if usr_inp == "justin" or "Eva":
print(usr_inp.greet())
应该是这样
usr_inp = input("Enter your name : ")
if usr_inp == "justin" or usr_inp == "eva":
print(Student(usr_inp).greet())