直接进入正题吧。
1.学生成绩题
tudent={"name":"樊","sno":11111,"sex":"female","result":"良好"}
print(student)
student.keys()
student.values()
score={"樊":{"数学":11,"语文":55,"c语言":85},"孙":{"数学":78,"语文":98,"c语言":82}}
sum=0
for i in score.values():
for j in i.values():
sum+=j
print("班级总分",sum)
print("班级平均分",sum/6)
score['樊']['数学']=60
print(score)
name=input('请输入要查询的名字:')
subject=input('请输入查询的科目:')
print(name,"的",subject,"result is :",score[name][subject])
2.水仙花数
A = 1
B = 0
C = 0
while True:
res = 100 * A + 10 * B + C
if res == A ** 3 + B ** 3 + C ** 3:
print(res)
C += 1
if C == 10:
C = 0
B += 1
if B == 10:
B = 0
A += 1
if A == 10:
break
也可以用另一种方法:
n = 100
while n <= 999:
ans = n
n0 = ans % 10
ans = (ans - n0) / 10
n1 = ans % 10
ans = (ans - n1) / 10
n2 = ans
if n0 ** 3 + n1 ** 3 + n2 ** 3 == n:
print(n)
n += 1
3.随机猜数
import random
print("The number is between 0 and 100")
answer = random.randrange(0,101)
count=0
while True:
count +=1
n=int(input("Input the number:"))
if n>answer:
print('too big')
elif n<answer:
print("too small")
else:
print('ok',answer,count)
break #只能猜一次 删除可以猜无数次
4.累加求N的和N可以是随便数
def cum_sum(N):
s = 0
for i in range(N):
s = s + i + 1
print(s)
cum_sum(5)
cum_sum(10)
cum_sum(15)
5.汉诺塔
def hano(N,source,target):
if N == 1:
print(1,source,'=>',target)
else:
pillars = ['A','B','C']
pillars.remove(source)
pillars.remove(target)
media = pillars[0]
hano(N - 1,source,media)
print(N,source,'=>',target)
hano(N - 1,media,target)
hano(4,'B','C')
ok,到这里就结束了,这是刚开始学Python的一些简单的代码希望对你们有用。