python代码怎么写,python代码编写

代码 0

python代码格式怎么写比较好

python代码格式怎么写比较好

建议就是用python的编辑器来进行python代码的编写,这样可以避免由于代码缩进问题给你带来的困扰,一般编辑器都会在保存代码的时候自动的检查出代码的缩进问题,很多
编辑器都会有自动的代码缩进功能

一句字符串 比如 我在干什么 把每个字按一行输出 怎么写 python 代码

print '\n'.join('what am I doing?')

用python写一个程序

用python写一个程序

其实这个不难,以下代码做个参考吧。
#-*-coding:utf-8-*-
class Student(object):
    """
    自定义Student类
    """
    def __init__(self, name, score):
    # 初始化Student,参数name,score
        self.name = name
        self.score = score
    def __cmp__(self, other):
    # 重写比较方法,根据Student类属性score进行比较
        return cmp(self.score, other.score)
    def show(self):
        return 'name:'+self.name+'; score:'+str(self.score)

def get_input():
    name = raw_input('input name > ')
    if not name: # 当姓名输入为空时,返回None
        return (None,None)
    score = raw_input('input %s\'s score > ' % name)
    if name and score:
        return (name,int(score)) # 为方便比较,将score强制转换为int型
    else:
        return (None,None)

def main():
    s_list = []
    while True: # 一直循环输入姓名及成绩
        name,score = get_input()
        if name and score:
            s = Student(name, score)
            s_list.append(s)
        else: # 当输入姓名或成绩为空时跳出循环
            break
    s_list.sort() # 对Student实体进行排序
    for i,s in enumerate(s_list): # 遍历已经排序的实体列表,并进行显示
        print ' '.join([str(i+1), s.name, str(s.score)])

if __name__ == '__main__':
    main()输入及输出结果如下,当然你也可以修改下 s_list 方便测试
input name > a
input a's score > 97
input name > b
input b's score > 95
input name > c
input c's score > 96
input name > 
1 b 95
2 c 96
3 a 97 注释已经比较详细了,应该能看懂。

标签: #开头 #源码 #源码 #复选框 #源码 #代码 #怎么看 #怎么看