a, b = 0, 1
while b < 10:
print(b,end=" ")
a, b = b, a+b
1 1 2 3 5 8
def Fib(n):
if n == 1 or n == 2:
return 1
else:
return Fib(n-1)+Fib(n-2)
n = int(input("请输入n:"))
a = Fib(n)
print("第n项值为:",a)
请输入n:3
第n项值为: 2
number = 7
guess = -1
print("数字猜谜游戏!")
while guess != number:
guess = int(input("请输入你猜的数字:"))
if guess == number:
print("恭喜,你猜对了!")
elif guess < number:
print("猜的数字小了...")
elif guess > number:
print("猜的数字大了...")
1)数字求和
num1 = input('输入第一个数字:')
num2 = input('输入第二个数字:')
sum = float(num1) + float(num2)
print('数字 {0} 和 {1} 相加结果为: {2}'.format(num1, num2, sum))
or
print('两数之和为 %.1f' %(float(input('输入第一个数字:'))+float(input('输入第二个数字:'))))
2)求1~100中所有奇数的和、所有偶数的和
num = 1; sub1 = 0; sub2 = 0
while(num <= 100):
if num % 2 == 0:
sub2 += num
else:
sub1 += num
num += 1 #while循环不要忘了!!
print("所以奇数和为%d,所以偶数和为%d" % (sub1,sub2))
#>>所以奇数和为2500,所以偶数和为2550 --,没有空格!!
3).求自然对数的底数e的值,直到最后一项的绝对值小于为止
i = 1; e = 1; t = 1
while (1/t >= pow(10,-6)): #pow(x,y)表示x的y次方
t *= i #t = t*i
e += 1/t
i += 1
print("e=",e)
4)显示100~200不能被3整除的数,要求一行显示10个数
j = 0 #控制一行显示的数值个数
print("100~200之间不能被3整除的数为:")
for i in range(100,201):
if i % 3 == 0:
continue
print(str.format("{0:<5}",i),end="") #每个数占5个位置,不足的后面加空格,不换行
j += 1
if (j % 10 == 0):
print() #换行
import math
r=int(input("请输入圆的半径:"))
c=2*math.pi*r
s=math.pi*r**2
print("请输入圆的周长%0.2f和面积%0.2f"%(c,s))
提示:v = b(1+r)**n
def value(b,r,n):
v = b * ((1 + r) ** n)
return v
total = value(1000,0.05,5)
print(total)
1)
for i in range(1,10): #外循环
s = ""
for j in range(1,10): #内循环
s += str.format("{0:1} * {1:1} = {2:<3}",i,j,i * j) #{0:1}表示索引为0,1个字符的长度
print(s)
1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9
2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 7 * 8 = 56 7 * 9 = 63
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
2)下三角
①可将1)中的for j in range(1,10)改为for j in range(1,i+1),其余不变
②
for i in range(1,10): #外循环
s = ""
for j in range(1,i+1): #内循环
print("%d * %d = %d"%(i,j,i * j),end="\t")
print("") #表示换行
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
判断一个正整数a是否为素数,只要判断a可否被2~中的任意一个整数整除即可,若不能则a为素数,否则为合数
1)简易:
a = int(input("请输入一个整数:"))
if a == 1 or a == 2 or a == 5 or a == 7:
print("yes")
if a % 2 == 0 or a % 3 == 0 or a % 5 == 0 or a % 7 == 0:
print("no")
else:
print("yes")
2)利用for循环和break语句
import math
a = int(input("请输入一个整数(>1):"))
k = int(math.sqrt(a))
for i in range(1,k+2):
if a % k == 0:
break
if i == k+1:
print("yes")
else:
print("no")
3)利用while循环和bool变量:
import math
a = int(input("请输入一个整数(>1):"))
k = int(math.sqrt(a))
flag = True #先假设所输整数为素数
i = 2
while (i <= k and flag == True): #bool类型记得首字母大写
if (a % i == 0):
flag = False #可以整除,肯定不是素数,结束循环
else:
i += 1
if (flag == True):
print("yes")
else:
print("no")
要求输入若干学生成绩(按Q或q键结束),如果成绩<0,则重新输入。统计学生人数和平均成绩。
num = 0; source = 0 #初始化学生人数和成绩之和
while True:
s = input("请输入学生成绩(按Q或q键结束):")
if s.upper() == "Q": #.upper() 将字符串小写字母-->大写字母
break
if float(s) < 0: #学生成绩要大于0
continue
num += 1
source += float(s)
print("学生人数为:{0},平均成绩为:{1}".format(num,source/num))