“无”一词在输出的每一行中不断出现

pyt*_*ice 0 python printing function object

对于课堂上的作业,我正在一个名为student()的类中创建对象。它涉及用户输入学生信息,然后以一种不错的格式输出学生信息。但是,控制台窗口中的每一行都会打印出“无”字样,并要求用户输入。我不确定为什么要打印出来,我想解决这个问题。

我相信问题出在我在定义函数init(自我)的地方,我在其中分配数据成员,但是我已经以多种方式更改了代码,但还没有运气。

class student(): 

     def __init__(self):

        self.name = input(print('What is the Student Name?: '))
        self.address = input(print('What is the Student address?: '))
        self.city = input(print('In which city does the Student reside?: '))
        self.state = input(print('In which state does the Student reside?: '))
        self.zip = input(print('In which zip code does the student reside?: '))
        self.id = input(print('What is the Student ID?: '))
        self.gpa = input(print('What is the Student GPA?: '))

        return

def formatInfo(list):

    for student in list:
        print('Student Name: ', student.name)
        print('Address: ', student.address)
        print('City: ', student.city)
        print('State: ', student.state)
        print('Zipcode: ', student.zip)
        print('Student ID: ', student.id)
        print('Student GPA: ', student.gpa)
        print('')

a = student()

b = student()

c = student()

student_list = [a,b,c]

formatInfo(student_list)
Run Code Online (Sandbox Code Playgroud)

我希望用户只会看到所提出的问题,而不会在问题旁边看到“无”字样。

Blo*_*ard 10

您不需要在print内部input调用-只需致电input

self.name = input('What is the Student Name?: ')
Run Code Online (Sandbox Code Playgroud)

What's happening is that print is a function which prints the string you pass it, but doesn't return anything.

You're passing the result of print to input, which prints what you pass it and then waits for input.

Since print returns nothing (after printing what you told it), input is printing None.