Python 使用print()无法获得所需的输出
我的python代码如下所示:Python 使用print()无法获得所需的输出,python,Python,我的python代码如下所示: class Student(): def __init__(self, name, branch, year): self.name = name self.branch = branch self.year = year # Variables name,branch and year are instance variables
class Student():
def __init__(self, name, branch, year):
self.name = name
self.branch = branch
self.year = year
# Variables name,branch and year are instance variables
# Different objects like Student1, Student2 will have diff values of these variables.
print('Student object is created for Student : ', name)
def print_details(self):
print('Name:', self.name)
print('Branch:', self.branch)
print('Year:', self.year)
Stud1 = Student('AAkash','ECE',2015)
Stud2 = Student('Vijay','IT',2017)
Stud1.print_details()
Stud2.print_details()
我的输出是:
('Student object is created for Student : ', 'AAkash')
('Student object is created for Student : ', 'Vijay')
('Name:', 'AAkash')
('Branch:', 'ECE')
('Year:', 2015)
('Name:', 'Vijay')
('Branch:', 'IT')
('Year:', 2017)
而在我所需的输出中,我需要如下语句:
Name : AAkash
Branch : CSE
您正在使用python-3.x打印语法,但可能只是在运行python 2.7
print('Name:', self.name)
您告诉print语句打印一个包含两个项的元组,而这正是它所做的
您可以删除括号,使其看起来像这样:
print 'Name:', self.name
print 'Branch:', self.branch
print 'Year:', self.year
这将打印您想要的内容(实际上打印多个项目,而不是元组本身)
或
通过从future(在python 2.7中)导入python3样式的打印函数,可以在代码中获得该函数:
它将为您提供所需的输出尝试从
print
statements@MooingRawr使用Python2,他将获得输出周围的括号。我想他不想要them@Georgy哦,我的错误,你是对的,显示了我是多么地使用Python2
from __future__ import print_function