1:计算1到10之和
result = 0
for i in range(11):
result += i
print(result)
2:输出2000到3000内的所有闰年
for i in range(2000,3001):
if(i%100 == 0):
if(i%400 == 0): print(i,end = ' ' )
elif(i%4 == 0): print(i, end = ' ')
3:计算Sn = 1-3+5-7+9+…
import math
result = 0
n = int(input("请输入一个正整数表示相加数的个数"))
for i in range (1,n+1):
result += pow(-1,i-1)*(2*i-1)
print(result)
4:打印九九乘法表(上三角、下三角、矩形块)
for i in range(1,10):
for j in range(1,i+1):
print(f"{j}×{i}={i*j}",end="\t")
print(end = '\n')
print(end = '\n')
for i in range(1,10):
for j in range(1,10):
if i <= 9:
print(f"{j}×{i}={i * j}", end="\t")
print("")
print(end = '\n')
for i in range(1,10):
for j in range(i,10):
print(f"{i}×{j}={i * j}", end="\t")
print(" ")
for i in range(1,10):
for j in range(1,i+1):
print("%d*%d=%d"%(j,i,j*i),end="\t")
print(end = '\n')
5:分别使用for、while循环求n!
def f(n):
if(n == 1): return 1
return f(n-1)*n
n = int(input("请输入一个整数:"))
if(n == 0): print(0)
else:
print(f(n))
n = int(input("请输入一个整数:"))
if(n == 0): print(0)
else:
result = 1
for i in range(2,n+1):
result *= i
print(result)
n = int(input("请输入一个整数:"))
if(n == 0): print(0)
else:
result = 1
while(n):
result *= n
n -= 1
print(result)
6:产生两个0~100的随机整数a,b,求这两个整数的最大公约数和最小公倍数
import random
def f(a,b):
if(a < b):a,b = b,a
if(b==0): return a
return f(a%b,b)
a = random.randint(0,100)
b = random.randint(0,100)
result1 = f(a,b)
result2 = a*b/result1
print(f'两个值分别是{a},{b},最大公约数是{result1},最小公倍数是{result2}')
7:实现下面程序:
while(1):
print("查询能量请输入能量来源!退出程序请输入0",end = '\n')
print("能量来源如下:")
print("生活缴费、行走捐、线下支付、网络购票")
a = input()
if(a == "生活缴费"):print("180g")
elif(a == "行走捐"):print("200g")
elif(a == "共享单车"):print("80g")
elif(a == "线下支付"):print("5g")
elif(a == "网络购票"):print("80g")
elif (a == "0"):break
8:
while(1):
print("——————10086查询功能——————",end = '\n')
print("输入a,查询当前余额",end = '\n')
print("输入b,查询当前剩余流量",end = '\n')
print("输入c,查询当前剩余通话",end = '\n')
print("输入d,退出自助查询功能",end = '\n')
a = input()
if(a == "a"):print("当前余额为:9999元",end = '\n')
elif(a == "b"):print("当前剩余流量为:100G",end = '\n')
elif(a == "c"):print("当前剩余通话为:200分钟",end = '\n')
elif(a == "d"):break
|