1、打印九九乘法表
i = 0 # 九九乘法表
while i < 9:
i += 1
j = 0
while j<i:
j +=1
print(f'{j}*{i}={i*j}',end='\t')
print()
输出结果:
2、用一到四组成不重复的三位数
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if(i!=k) and (i!=j) and (j!=k):
print (i,j,k)
输出结果:
3、任意输入一个数判断是奇数还是偶数
num=int(input('please input a number:'))
if num%2 == 0:
print(num,'is an even')
else:
print(num,'is an odd')
输出如下:
4、输入一个年份判断是平年还是润年
year = int(input('please input a year:'))
if (year % 4 == 0 and year % 100 !=0) or year % 400 ==0:
print('leap year')
else:
print('ordinary year')
输出如下:
5、
'''# 狗前两年相当于人10.5岁,然后没增加一年就增加4岁'''
dog_age = float(input('please input thr age of dog:'))
person_age=0
if dog_age>0:
if dog_age<=2:
person_age =dog_age*10.5
else:
person_age=10.5*2
person_age+=(dog_age-2)*4
print('the dog of {} years old is eual of {} years old '.format(dog_age,person_age))
else:
print('ERROR')
输出如下:
6、求100内所有偶数的和
'''# 求一百之内所有偶数的和'''
i = 0
result = 0
while i<100:
i+=1
if i % 2 == 0:
result += i
print('the rusult is {}'.format(result))
输出如下:
7、找出1000以内的水仙花数
'''获取1000以内的水仙花数'''
i = 100
while i<1000:
#假设i的百位是a,十位数是d,个位是c
a = i // 100
b = (i//10) % 10
c = i % 10
if a**3+b**3+c**3 == i:
print(i)
i+=1
输出如下:
8、判断一个数是否为质数
'''获取用户输入的任意数,判断其是否为质数'''
num = int(input('please input a number which is bigger than 1:'))
i = 2
flag = 1
while i < num :
if num % i ==0:
flag = 0
i +=1
if flag:
print(num,'is a prime')
else:
print(num,'is not a prime')
输出如下:
9、
'''
打印出以下图形
*
**
***
****
*****
'''
i = 0
while i<5:
i +=1
print('*'*i)
输出如下:
10、
i = 0
while i < 5:
i += 1
print('*'*(5-i))
输出如下: