class Friend:
def __init__(self, name, phone):
self.name = name
self.phone = phone
def get_name(self, input_name):
self.name = input_name
def get_phone (self, input_phone):
self.phone = input_phone
name = input ('Enter name:')
phone = input ('Enter phone number:')
f = Friend(name,phone)
print(f.get_name(name))
print(f.get_phone(phone))
When you run
None
None
It pops up like this. What's the problem and what's the solution?
python
Because there is no return value.
Methods with "get_" are commonly used as methods with "set_".
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
This is because the function has no return value. Try to modify it in the following way.
class Friend:
def __init__(self, name, phone):
self.name = name
self.phone = phone
def get_name(self, input_name):
self.name = input_name
return self.name
def get_phone (self, input_phone):
self.phone = input_phone
return self.phone
name = input ('Enter name:')
phone = input ('Enter phone number:')
f = Friend(name,phone)
print(f.get_name(name))
print(f.get_phone(phone))
Or
class Friend:
def __init__(self, name, phone):
self.name = name
self.phone = phone
def get_name(self, input_name):
print(input_name)
def get_phone (self, input_phone):
print(input_phone)
name = input ('Enter name:')
phone = input ('Enter phone number:')
f = Friend(name,phone)
f.get_name(name)
f.get_phone(phone)
543 Uncaught (inpromise) Error on Electron: An object could not be cloned
747 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
552 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
551 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.