我的python程序得到输入和输出是空白的

时间:2015-11-04 15:26:38

标签: python input output

import os
import time

class student:
    def __init__(self):
    self.name=""

    def inp(self):
        print "This is student record program"
        self.Name = raw_input("Enter the name: ")
        self.ID = input("Enter the ID :")
        self.Age = raw_input("Enter the age: ")
        self.Mark = [input("Enter marks one by one ") for i in range(3)]
        self.total = sum(self.Mark)

    def dis(self):
        print "Name: ".format(self.Name)
        print "ID: ".format(self.ID)
        print "Age: ".format(self.Age)
        for i in range(3):
            print "Mark :".format(self.Mark[i])
        print "Total: ".format(self.total)

stu1=student()
stu1.inp()
stu1.dis()

这是我的代码。请帮我。 我试图获取学生的详细信息并使用课程进行打印 但输出是空白的 我是python的新手 我不知道我哪里出错了。

2 个答案:

答案 0 :(得分:1)

    print "Name: ".format(self.Name)

format字符串需要大括号来指示插入值应该去的位置。

    print "Name: {}".format(self.Name)

添加括号后,输出将为:

This is student record program
Enter the name: a
Enter the ID :1
Enter the age: 2
Enter marks one by one 1
Enter marks one by one 2
Enter marks one by one 3
Name: a
ID: 1
Age: 2
Mark: 1
Mark: 2
Mark: 3
Total: 6

答案 1 :(得分:0)

您可以使用python字符串格式化运算符 %s %d %f etc

String Formatting Operators

def dis(self):
    print "Name: %s" % self.Name
    print "ID: %s" %self.ID
    print "Age: %s" % self.Age
    for i in range(3):
        print "Mark : %s" % self.Mark[i]
    print "Total: %s" % self.total