当前位置:首页>开发>正文

用python写一个程序 用python写一个程序

2023-06-28 20:51:12 互联网 未知 开发

 用python写一个程序 用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 %ss 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 as score > 9input name > b
input bs score > 9input name > c
input cs score > 9input name > 
1 b 92 c 93 a 97 注释已经比较详细了,应该能看懂。

用python写一个程序

for gj in range(21):
   for mj in range(34):
       for xj in range(101):
           if (gj mj xj==100) and (gj*5 mj*3 xj/3==100):
               print(gj,mj,xj)

python3 写个最简单的程序

用split()分割即可
a = input(input a and b)
lista = a.split(,)
ans = (int)(lista[0])   (int)(lista[1])
print(ans)

python的一个简单程序

#-*- coding:utf-8 -*-

name = raw_input(Please enter your name: )

if name == Tom:
print You are great!
else:
print You input the wrong name!

请问谁会python能帮我编写个简单的程序么,不胜感激,祝您新年愉快大吉大利。

假定id是开头的数字,line是后面的一个句子,items是所有的行,已全部读入内存
def build_index(items):

rev_index = collections.defaultdict( lambda: set() ) #反向索引

for id, line in items:
terms = line.strip().split()
for t in terms:
rev_index[t].add(t)
return rev_index

反向索引的这个数据结构是以set为默认构造函数的defaultdict类型,这个算是python的一点技巧,其它的部分都很简单。

帮忙写一个简单的Python程序

 

import math

#构造符合前2个条件的数据

def huoqu():

l=[]

for i in range(10000):

b=str(i)

if len(b)<4:

n=(4-(len(b)))*0 b

if n[0]==n[1] and n[2]==n[3] and n[0]!=n[2]:

l.append(n)

else:

if b[0]==b[1] and b[2]==b[3] and b[0]!=b[2]:

l.append(b)

return l

#构造符合第三个条件的函数

def panduan(n):

for i in n:

b=math.sqrt(int(i))

#确保结果是1个整数而不是小数

if b-int(b)==0:

return i

print(panduan(huoqu()))

最后输出的结果是7744

谁能帮我用python编下面几个小程序?(初学者的)



def printStar(num):
    print thr star matrix.
    if not(num>0 and (num-1)%2==0):
        print str(num) " is not the odd number, which larger than 0."
        return
    blocknum=(num-1)/    block=
    star=*
    for i in range(num):
        if i<=blocknum:
            line=
            line =block*(blocknum-i) star*(1 2*i) block*(blocknum-i)
            print line
        else:
            line=
            line =block*(i-blocknum) star*(num-2*(i-blocknum)) block*(i-blocknum)
            print line
if __name__==__main__:
    printStar(8)
    printStar(-13)
    printStar(9)

怎样用python实现这样一个简单程序

def numberInputer(prompt, quitflat):
    """ prompt: 输入时的提示信息,
        quitflag: 退出标识字
    """
    while True:
        gotter = rawinput(prompt , "%s" for quit % quitflag)
        if gotter==quitflag:
            break
        try:
            yield int(gotter)
        except:
            continue

degrees = []
for degree in numberInputer(prompt="请输入考试分数", quitflag="Q"):
    degrees.append(degreee)
print "分数中大于60的分数之和", sum(filter(lambda x: x>=60, degrees))

最新文章