共 9 篇文章 |
|
通过Python为指字文件添加内容#! /usr/bin/python3# _*_ coding:UTF-8 _*_#同一目录下保存文件,open是打开这个指定文件传递给fofo= open("foo.txt","w")#输出foo的文件名字,fo.name是open后面的文件名。#内容赋值给str1str1 ="菜鸟教程,努力学就能学得会哦!"#fo.write(字符变量)输出write后面内容fo.write(s... 阅43 转0 评0 公众公开 19-11-13 11:14 |
计算五年复利率的实例#本金是30000principal=30000#十个月年利率是0.10元rate=0.10#五年的复利numyears=5#一年时间year=1#循环计算的复利率while year<=numyears: #本金乘以1+利率 principal=principal*(1+rate) #输出利率结果 print("%5d %0.2f" % (year,principal)) #时间累加1 year +=1解析:%5d:将一个整数格式化... 阅169 转0 评0 公众公开 19-11-13 10:24 |
连加法编程def getsum(*a) : s=0 for item in a : s+=item return s;print(getsum(1,2))print(getsum(1,2,3))print(getsum(1,2,3,4)) 阅44 转0 评0 公众公开 19-11-12 10:13 |
利用定义的函数编多个参数相加的程序#定义多参数相加:*args#定义函数名:calc_sum#ax = 0,相加结果的存入局部变量#for n in args: 从多个参数中提取元素值依次与ax累加#return ax,最终结果导出#调用函数,让1,5,8,相加。 阅74 转0 评0 公众公开 19-11-07 22:34 |
写一个小的判断酒驾程序proof = int(input("输入驾驶员每 100ml 血液酒精的含量:"))if proof <20: print("驾驶员不构成酒驾")else: if proof <80: print("驾驶员已构成酒驾") else: print("驾驶员已构成醉驾") 阅73 转0 评0 公众公开 19-11-07 06:57 |
表格代码name=input("请输入姓名:")che_grade=input("请输入语文成绩:")math_grade=input("请输入数学成绩:")print("{0:10}{1:>6}{2:>5}".format("姓名","语文","数学"))print("{0:10}{1:>6}{2:>5}".format(name,che_grade,math_grade))花括... 阅33 转0 评0 公众公开 19-10-12 14:15 |
正确的格式:name=input()if name == "Alice": print("Hello,Alice")elif age <12: print("You are not Alice")elif age >2000: print("You are not alse Alice")elif age >1000: print("Don''''''''t pretend to be Alice") 阅50 转0 评0 公众公开 19-10-11 15:41 |