kcjf.net
当前位置:首页 >> 谁可以帮我写一个python小程序? >>

谁可以帮我写一个python小程序?

import math #导入模块/库 for i in range(1,10): #for循环语句 print math.sin(i) #print 输出 for j in range(100): if j%2==0: #if 条件判断语句 print j,"是偶数" else: print j,"是奇数" data=[1,2,'c','d',5] #列表 print data[2] #列表索引,输出结果为c#循环、判断、列表等等,这是最基础的

写一个python的小程序.输出要为Please enter the number of Epidemic cards (4-6) >start: No epidemics, Infection Rate=2when Epidemics=1, Infection Rate=2when Epidemics=2, Infection Rate=2when Epidemics=3, Infection Rate=3when

a=input('请输入一个用逗号隔开的九个数字:')b=[]for i in a.split(','): b.append(int(i))b.sort(reverse=False)print(b) 以上代码在python 3.1中通过.在2.6中可以将input用raw_input替代即可.

print "plz input a number:"n=raw_input()s=str(n)print "n的位数是"+str( len(s) )print "正序输出n:"print nprint "逆序输出n:"print int( s[::-1] )思路就是先把数字转换为字符串,用字符串的操作来实现求位数和逆序,输出的时候再把字符串转为整数. 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)/2 block=' ' star='*' for i in range(num): if i<=blocknum: line='' line+=block*(blocknum

def cal(input="input.txt",output="output.txt"): #cal方法为主程序,推荐这样做而不是python.exe xx.py xxx xxx.默认参数为python目录的两个txt,如为其他文件自己指定. infile = file(input,"r")#打开源数据文件 outfile = file(output,"w")#打开

import randomclass StockGame: players = {} stocks = {} logs = [] events = {} rounds = 0 def __init__(self): pass def add_player(self, name, money): self.players[name] = { 'name': name, 'initm': money, 'money': money, 'stock':{} } for s in self.stocks: self

import sysCa=0.34Cr = float(input("请输入Cr:"))if Cr<0.06: print("gas-emission scope error") sys.exit(0)m = float(input("请输入m:"))Q=60*m*(Cr-Ca)/0.66print("Q的值为:"+str(Q))T=float(input('请输入T的值:'))W=Q*Tprint("W的值为:"+str(W))

这个是 python2.7 的代码,如果是 python3.x 的话,改一下 print 函数就好.if __name__ == '__main__': list_three = [] list_six = [] list_nine = [] for i in range(1, 301): if i % 3 == 0 or str(i).find('3') -1: list_three.append(i) if i % 6 == 0 or str(i).find('6') -1:

可能会有的错误:OverflowError: Python int too large to convert to C long或者OverflowError: range() result has too many items,python的Int无限制,但C的int和long都会溢出,因为python的很多函数底层用C实现的,所以python有时也会出现这种错误,解决办法就是不要使用这些函数,改进算法,用别的函数在需要用for x in range(很大的整数) 来写循环时, 用while 来代替

网站首页 | 网站地图
All rights reserved Powered by www.kcjf.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com