首页 > 解决方案 > “None”这个词不断出现在输出的每一行

问题描述

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

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

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)

我希望用户只会看到被问到的问题,而不是问题旁边的“无”字样。

标签: pythonfunctionobjectprinting

解决方案


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

self.name = input('What is the Student Name?: ')

正在发生的事情是print一个函数,它打印你传递给它的字符串,但不返回任何东西。

您正在传递printto的结果input,它会打印您传递的内容,然后等待输入。

由于print什么都不返回(在打印您告诉它的内容之后),input正在打印None.


推荐阅读