一、#澳币转换
人民币和澳币之间转换
汇率人民币:澳币=5:1
人民币缩写RMB,rmb
澳币缩写AUD,aud
格式化输出转换结果为2位小数
拓展程序要求:引入循环实现多次转换,以用户输入N或者n结束转换
print("若输入的币值带'N'或'n'则默认退出")
Convert=input("请输入带有符号的币值:")
while Convert[-1] not in ['N','n']:
if Convert [-3:] in ['RMB','rmb']:
c=(eval(Convert[0:-3]))/5
print("转换后的币值是{:.2f}AUD".format(c))
elif Convert[-3:] in ['AUD','aud']:
a=(eval(Convert[0:-3]))*5
print("转换后的币值是{:.2f}RMB".format(a))
else:
print ("输入格式错误")
Convert = input("请输入要转换的金额:")
或者
while 1:
str1 = input("请输入你想要转换的货币:")
# 如果用户输入 "N" 或 "n" 退出程序
if str1 == "N" or str1 == "n":
break
else:
if str1[-3:] in ["RMB", "rmb"]:
RMB = eval(str1[0: -3])
AUD = RMB / 5
print("{:.2f} RMB转换为{:.2f} AUD".format(RMB, AUD))
elif str1[-3:] in ["AUD","aud"]:
AUD = eval(str1[0: -3])
RMB = AUD * 5
print("{:.2f} AUD转换为{:.2f} RMB".format(AUD, RMB))
else:
print("输入不正确请重新输入")
或者(温度转换)
print("若输入的币值带'N'或'n'则默认退出")
TempStr = input("请输入带有符合的温度值:")
while TempStr[-1] not in ['N','n']:
if TempStr[-1] in ['F','f']:
C = (eval(TempStr[0:-1]) - 32)/1.8
print("转换后的温度是{:.2f}C".format(C))
elif TempStr[-1] in ['C','c']:
F = 1.8*eval(TempStr[0:-1]) + 32
print("转换后的温度是{:.2f}F".format(F))
else :
print("输入格式错误")
TempStr = input("请输入带符号的温度:")
二、年终值
N=0
while N<0.01:
N=N+0.001
daydayup=1.0
for i in range(365):
if i % 7 in (6,0):
daydayup = daydayup
else:
daydayup = daydayup * (1 + N)
print("{:.2f}".format(daydayup))#四天的天天向上年终值
w =input('N的参数:')
dayup=1.0
ddup=eval(w)
for i in range(365):
if i % 7 in [1,2,3,4]:
dayup=dayup*(1+ddup)
print('连续四天的工作年终值为:{:.2f}'.format(dayup))
#五天的天天向上年终值
tt =input('N的参数:')
daybg=1.0
ddup=eval(tt)
for i in range(365):
if i % 7 in [1,2,3,4,5]:
daybg=daybg*(1+ddup)
print('连续五天的工作年终值为:{:.2f}'.format(daybg))
#六天的天天向上年终值
w =input('N的参数:')
dayup=1.0
ddup=eval(w)
for i in range(365):
if i % 7 in [1,2,3,4,5,6]:
dayup=dayup*(1+ddup)
print('连续六天的工作年终值为:{:.2f}'.format(dayup))
#360天的天天向上年终值
dayfactor =input('N的参数:')
dayup=1
ddup=eval(dayfactor)
for i in range(360):
if i%30 in [1,2,3,4,5,6,7,8,9,10]:
dayup=dayup*(1+ddup)
print("月初连续十天的工作年终值为:{:.2f}".format(dayup))
三、BMI值
通过输入身高和体重 计算BMI值 并根据国际和国内标准判断指标。
height, weight = eval(input("请输入身高(米)和体重(公斤)[逗号隔开]: "))
bmi = weight / pow(height, 2)
print("BMI 数值为:{:.2f}".format(bmi))
who, dom = "", ""
if bmi < 18.5:
who, dom = "偏瘦", "偏瘦"
elif 18.5 <= bmi < 24:
who, dom = "正常", "正常"
elif 24 <= bmi < 25:
who, dom = "正常", "偏胖"
elif 25 <= bmi < 28:
who, dom = "偏胖", "偏胖"
elif 28 <= bmi < 30:
who, dom = "偏胖", "肥胖"
else:
who, dom = "肥胖", "肥胖"
print("BMI 指标为:国际'{0}', 国内'{1}'".format(who, dom))
四、成绩分级
通过输入成绩判断所对应的级别
score = eval(input("请输入分数:")) #提示用户输入成绩
if score>= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
elif score < 60:
grade = "不及格"
print("输入成绩属于级别{}".format(grade))
五、猜0~9之间一个整数
猜数游戏。 在程序中预设一个0-9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了”;小于预设的数,显示“遗憾,太小了”;如此循环,直至猜中该数,显示“预测N次,你猜中了!”,其中N是用户输入数字的次数
import random
N = 0
num2 = random.randint(0,9)
while 1 > 0 :
num1 = eval(input("请输入一个0到9之间的整数:"))
N += 1
if num1 > num2:
print("遗憾,太大了")
elif num1 < num2 :
print("遗憾,太小了")
else:
print("恭喜你,猜了",(N) ,"次对了")
break;
六、写一个数字8
请用turtle库画数码管数字8
import turtle
def drawGap():
#绘制数码管间隔
turtle.penup()
turtle.fd(8)
def drawLine(draw):
drawGap()
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
drawGap()
turtle.right(90)
def drawDigit(w):
drawLine(True) if w in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if w in [0,1, 3, 4, 5, 6,7, 8, 9] else drawLine(False)
drawLine(True) if w in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if w in [0,2,6,8] else drawLine(False)
turtle.left(90)
drawLine(True) if w in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if w in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if w in [0,1,2,3,4,7,8,9] else drawLine(False)
turtle.left(180)
turtle.penup()
turtle.fd(20)
def drawDate(date):#获得要输出的数字
for i in date:
drawDigit(eval(i))
def main():#主函数辅助功能
turtle.setup(800,350,200,200)
turtle.penup()
turtle.fd(0)
turtle.pencolor("red")
turtle.pensize(5)
drawDate('8')
turtle.hideturtle()
turtle.done()
main()#调用主函数
七、(偶数)计时器 七段数码时钟,至少实现秒的跳动功能
import turtle, time
def drawGap():
turtle.penup()
turtle.fd(5)
def drawLine(draw):
drawGap()
turtle.pendown() if draw else turtle.penup()
turtle.fd(40)
drawGap()
turtle.right(90)
def drawDigit(w):
drawLine(True) if w in [2,3,4,5,6,8,9] else drawLine(False)
drawLine(True) if w in [0,1, 3, 4, 5, 6,7, 8, 9] else drawLine(False)
drawLine(True) if w in [0, 2, 3, 5, 6, 8, 9] else drawLine(False)
drawLine(True) if w in [0,2,6,8] else drawLine(False)
turtle.left(90)
drawLine(True) if w in [0,4,5,6,8,9] else drawLine(False)
drawLine(True) if w in [0,2,3,5,6,7,8,9] else drawLine(False)
drawLine(True) if w in [0,1,2,3,4,7,8,9] else drawLine(False)
turtle.left(180)
turtle.penup()
turtle.fd(20)
def drawDate(date):
turtle.pencolor('red')
for i in date:
if i == '-':
turtle.write('时',font=('Arial',18,'normal'))
turtle.pencolor('green')
turtle.fd(40)
elif i == '=':
turtle.write('分', font=('Arial', 18, 'normal'))
turtle.pencolor('blue')
turtle.fd(40)
elif i == '+':
turtle.write('秒', font=('Arial', 18, 'normal'))
turtle.pencolor('yellow')
else:
drawDigit(eval(i))
def init():
turtle.setup(800,350,600,200)
turtle.speed(10)
turtle.penup()
turtle.goto(0,0)
turtle.fd(-350)
turtle.pensize(5)
def main():
while True:
turtle.clear()
init()
timedate = time.strftime("%H-%M=%S+", time.localtime())
turtle.getscreen().tracer(30,0)
drawDate(timedate)
time.sleep(1)
turtle.hideturtle()
main()
八、(奇数)倒计时 七段数码管倒计时9-0
from turtle import *
import time
def Line(bool):
penup()
fd(5)
if bool :
pendown()
else:
penup()
fd(40)
penup()
fd(5)
right(90)
def Digit(digit):
if digit in [2,3,4,5,6,8,9]:
Line(1)
else:
Line(0)
if digit in [0,1,3,4,5,6,7,8,9]:
Line(1)
else:
Line(0)
if digit in [0,2,3,5,6,8,9]:
Line(1)
else:
Line(0)
if digit in [0,2,6,8]:
Line(1)
else:
Line(0)
left(90)
if digit in [0,4,5,6,8,9]:
Line(1)
else:
Line(0)
if digit in [0,2,3,5,6,7,8,9]:
Line(1)
else:
Line(0)
if digit in [0,1,2,3,4,7,8,9]:
Line(1)
else:
Line(0)
left(180)
penup()
fd(30)
def Date(second):
for i in range(second,-1,-1):
reset()
hideturtle()
tracer(False)
pensize(5)
pencolor("red")
Digit(i)
update()
time.sleep(1)
def main():
setup(1000,500,200,200)
penup()
Date(int(9))
done()
speed(0)
main()
九 平均值 中位数 标准差 最大值 最小值
请从控制台获取多个数字,直到输入回车退出数字获取,然后利用函数的模块化设计求得这组数字统计值:平均值mean,中位数median,标准差dev,请用定义以下函数,以调用函数的形式完成编码
getNum()函数:用于循环从控制台获取用户输入的数字,当用户按Enter键是退出,所有数据保存在列表ls中。 列表初始化时定义为空,而后根据输入逐渐增加其长度。
mean()函数:用浮点数sum记录列表所有元素求和的结果。其中,for语句表示从列表ls中取出每一个元素,将其加到sum变量中,直到ls中的最后一个元素。最后,通过return语句返回平均值,len(ls)用于计算列表的长度。
dev()函数:将mean()函数求得的平均值作为一个参数输入给标准差dev()函数,再根据标准差公式求得对应标准差,注意函数pow()或sqrt()的用法
median()函数:首先使用python内置函数sorted()对列表中数字进行排序,然后根据中位数定义计算中位数。
from math import sqrt
def getNum(): #获取用户输入
nums=[]
iNumStr=input("请输入数字(直接输入回车退出): ")
while iNumStr!="":
nums.append(eval(iNumStr))
iNumStr = input("请输入数字(直接输入回车退出): ")
return nums
def mean (numbers) : #计算平均值
x=0.0
for num in numbers:
x=x+num
return x /len (numbers)
def dev (numbers, mean) : #计算方差
sdev = 0.0
for num in numbers:
sdev=sdev+(num-mean)**2
return sqrt(sdev / (len (numbers)-1))
def median (numbers) : #计算中位数
new = sorted (numbers)
size = len (numbers)
if size%2==0:
med = (new[size//2-1] + new[size//2])/2
else:
med = new[size//2]
return med
def max (numbers): #计算最大值
y=numbers[0]
for num in numbers:
if y<num:
y=num
return y
def min (numbers): #计算最小值
y=numbers[0]
for num in numbers:
if y>num:
y=num
return y
n = getNum() #主体函数
m = mean(n)
print("平均值:{},方差:{:.2},中位数:{},最大值:{},最小值:{}" .format(m,dev(n,m) ,median(n),max(n),min(n)))
十、三国演义
首先下载jieba库 :\>pip install jieba
编写程序 使程序和文件在同一文件夹(注意文件txt编码)
import jieba
txt = open("threekingdoms-utf8.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
import jieba
txt =open("threekingdoms.txt","r",encoding='utf-8').read()
words=jieba.lcut(txt)
counts={}
for word in words:
if len(word)==1:
continue
else:
counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(15):
word,count=items[i]
print("{0:<10}{1:>5}".format(word,count))
• 三角形绘制
叠加等边三角型
import turtle
turtle.pendown()
turtle.fd(100)
turtle.right(120)
turtle.fd(100)
turtle.right(120)
turtle.fd(100)
turtle.right(60)
turtle.fd(100)
turtle.right(120)
turtle.fd(200)
turtle.right(120)
turtle.fd(200)
turtle.right(120)
turtle.fd(100)
turtle.right(60)
或者
import turtle
turtle.seth(60)
turtle.fd(90)
turtle.seth(-60)
turtle.fd(180)
turtle.seth(180)
turtle.fd(180)
turtle.seth(60)
turtle.fd(90)
turtle.seth(0)
turtle.fd(90)
turtle.seth(-120)
turtle.fd(90)
turtle.seth(120)
turtle.fd(90)
turtle.seth(0)