python - “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)
我希望用户只会看到被问到的问题,而不是问题旁边的“无”字样。
解决方案
您不需要在print
内部input
调用 - 只需调用input
:
self.name = input('What is the Student Name?: ')
正在发生的事情是print
一个函数,它打印你传递给它的字符串,但不返回任何东西。
您正在传递print
to的结果input
,它会打印您传递的内容,然后等待输入。
由于print
什么都不返回(在打印您告诉它的内容之后),input
正在打印None
.
推荐阅读
- vba - 在 PDF 中搜索关键字并给出页码
- windows - ffmpeg 到 Windows 上的 nginx rtmp 出现 I/O 错误
- mysql - 为什么 MySQL 不接受 GEOMETRY 列的这个 WKT 值?
- plot - rqt_plot 无法接收任何数据
- java - @Embeddable 中的 @GeneratedValue
- google-bigquery - Google Bigquery ML 模型架构 ValueError
- stanford-nlp - 无法使用 Shift-Reduce Parser 启动 Stanford CoreNLP 服务器
- python - 在变量声明中使用冒号
- iis - 安装 Server Core 2016 后如何引导至 Windows 10
- makefile - 获取 travisCI 构建环境,用于在 R 包中构建的依赖于 netcdf 的 Fortran