I'm getting the error "AttributeError: 'NoneType' object has no attribute 'name'" for the code below:
class Student:
def __init__(self, name, house):
if not name:
raise ValueError("Missing name!")
self.name = name
self.house = house
def main():
student = get_student()
print(f"{student.name} from {student.house}")
def get_student():
name = input("Name: ")
house = input("House: ")
try:
return Student(name, house)
except ValueError:
print("Please make sure to enter a name...")
main()
if __name__ == "__main__":
main()
I was hoping that the main() call inside get_student() function will create a recursion-like effect and will allow the program to go on until both 'name' and 'house' have been inserted. However, what happens is a little different.
When I enter name and house, the program works. When I omit entering a name, it does print the ValueError message in get_student() and prompts for another set of inputs.. but, then I get "AttributeError: 'NoneType' object has no attribute 'name'".
Am I doing something completely stupid? Any help is much appreciated.