[英]How to use an input to call class function?
Here is my code:这是我的代码:
class People():
def __init__(self, character_name, character_age):
self.character_name = character_name
self.character_age = character_age
@classmethod
def from_input(cls):
return cls(input('Name: '), int(input('Age: ')))
def introduce(self):
print(f'Hey! I am {self.name}. I am {self.age}.')
search = input('Search: ')
search.introduce()
Obviously, search.introduce()
doesn't work.显然,
search.introduce()
不起作用。 What should I change to have the desired output?我应该更改什么以获得所需的 output?
You must apply the following changes:您必须应用以下更改:
class People():
def __init__(self, character_name, character_age):
self.character_name = character_name
self.character_age = character_age
@classmethod
def from_input(cls):
return cls(input('Name: '), int(input('Age: ')))
def introduce(self):
print(f'Hey! I am {self.character_name}. I am {self.character_age }.')
search = People.from_input()
search.introduce()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.