练习1 2019-3-19
# 写一个函数实现99乘法表 def x99(x): if x >=1 and x <=9: line = 1 while line <= x: start=1 while start <= line: print('{0}*{1}={2}'.format(start,line,start*line),end=' ') start+=1 print() line+=1 else: print('参数在1-9的正整数范围内!') # 调用函数 x99(9)
练习2 2019-3-19
# 1到4能组成多少个互不相同且不重复数字的三位数?分别是? for x in range(1,5): for y in range(1,5): for z in range(1,5): if x != y and y != z and x != z: print(x,y,z)
练习3 2019-3-19
# 计算两个列表对应相加的和,输出一个新的列表 a = [1,2,3] b = [4,5,6] c = [] for i in range(len(a)): c.append(a[i]+b[i]) print(c)
练习4 2019-3-19
# 输出一个三角形 def sjx(x): for i in range(1,x): print("*"*i) sjx(4) print('-'*50) #输出一个空心正方形 def zfx1(x): print("*"*x) for i in range(x-2): print("*"+" "*(x-2)+"*") print('*'*x) zfx1(4) print('-'*50) #输出一个实心正方形 def zfx2(x): for i in range(x): print("*"*x) zfx2(4) print("-"*50)
练习5 2019-3-19
# 1、一个不超过5位数的正整数,判断位数,且输出个十百千万位上的数 num = int(input("不超过5位数的正整数: ")) n = str(num) # 按转字符串方式去获取 if len(n) > 5: print("请输入一个不超过5位数的正整数!") else: for i in n[::-1]: print(i) print('-'*50) # 按数字去计算获取 # for i in range(len(n)): # print(num%10) # num = num//10
# 2、输入一个数。计算出这个数以内的整数之和 def sum(x): s1 = 0 for i in range(x+1): s += i print(s) sum(5)
# 3、求可用被17整除的所有三位数 n = [] for i in range(99,1000): if i % 17==0: n.append(i) print(n)
练习6 2019-3-19
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/3/19 17:18 # @Author : wangdecheng # @Software: PyCharm import time,datetime # 输入年月日,判断这一天是这一年中的第几天? # 方法1 # y = int(input('year:\n')) # m = int(input('month:\n')) # d = int(input('day:\n')) # targetDay = datetime.date(y, m, d) # dayCount = targetDay - datetime.date(targetDay.year - 1, 12, 31) # print('%s是%s年的第%s天。' % (targetDay, y, dayCount.days)) # 78天 # 方法2 a=[[31,28,31,30,31,30,31,31,30,31,30,31], [31,29,31,30,31,30,31,31,30,31,30,31]] #a[0]普通年的月天数,a[1]闰年的月天数 year=int(input("年:")) month = int(input("月:")) day = int(input("日:")) sum=0 if (year%4==0 and year%100!=0)or year%400==0: for i in range(1,month): sum=sum+a[1][i] else: for i in range(1,month): sum=sum+a[0][i] sum=sum+day print("{}年{}月{}日是这一年的第{}天".format(year,month,day,sum))