Python习题006
作业一:打印10*10 星星 ★☆
- 要求一:普通打印★
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 l = 0 2 while l <10: 3 h = 0 4 while h < 9: 5 print("★", end=" ") 6 h += 1 7 print("★") 8 l+=1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 打印 10 * 10 的星星表格 2 # 这种写法速度比较快 3 n = 0 4 while n < 100: 5 print('★', end=' ') 6 if n % 10 == 9: 7 print() 8 n += 1
- 要求二:隔列变色星星★☆
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def start1(): 2 n = 0 3 while n < 100: 4 if n % 2 == 0: 5 print('★', end=' ') 6 # 否则 7 else: 8 # 打印空心,不换行 9 print('☆',end=' ') 10 # 打印十颗星星后换行 11 if n % 10 == 9: 12 print() 13 # 每次自增一次 14 n += 1 15 start1()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 打印 10 * 10 的星星表格 2 # 隔列变色★☆ 双循环 3 def start3(): 4 n = 0 5 # 行循环 10 次 6 while n < 10: 7 j = 0 8 # 列循环 10 次 9 while j <10: 10 # 列为偶数时,为空心星 11 if j % 2 ==0: 12 print('☆', end=' ') 13 else: 14 print("★", end=" ") 15 # 列自加 1 16 j+=1 17 # 换行 18 print() 19 # 行自加 1 20 n += 1 21 start3()
- 要求三:隔行变色星星★☆
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def start2(): 2 n = 0 3 while n < 100: 4 # 对10取商,在对2求余 5 if n // 10 % 2 == 0: 6 print('★', end=' ') 7 # 否则 8 else: 9 # 打印空心,不换行 10 print('☆',end=' ') 11 # 打印十颗星星后换行 12 if n % 10 == 9: 13 print() 14 # 每次自增一次 15 n += 1 16 start2()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 隔行变色★☆ 双循环 2 def start4(): 3 l = 0 4 # 对行循环 10 次 5 while l < 10: 6 h = 0 7 # 对列循环 10 次 8 while h < 10: 9 # 行为偶数时,为空心星 10 if l % 2 == 0: 11 print('☆', end=' ') 12 else: 13 print("★", end=" ") 14 # 列自加 1 15 h+=1 16 print() 17 # 行自加 1 18 l += 1 19 start4()
作业二:打印九九乘法表
- 方法一
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def m_table1(): 2 for i in range(1,10): 3 for j in range(1,i+1): 4 print(j ,"*", i ,"=",i*j,end='\t') 5 print() 6 m_table1()
- 方法二
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 定义变量 n 乘数 m 被乘数 2 def m_table2(): 3 n = 1 4 while n < 10: 5 m = 1 6 while m <= n: 7 print(m,"*",n,"=",n*m,end="\t") 8 m += 1 9 print() 10 n += 1 11 m_table2()
更多方法点击链接,网上有好多方法百度一下,你就知道:https://www.cnblogs.com/pychina/p/10171922.html
作业三:简易版计算器 + - * / % // 输入一个数,输入第二个数,进行加减乘除操作...
- 方法一
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def in_put(): 2 ''' 3 定义输入要计算的数 4 :return: 返回数字1和数字2 5 ''' 6 one_number = float(input("请输入第一个数字:")) 7 two_number = float(input("请输入第二个数字:")) 8 return one_number,two_number 9 def addition(x,y): 10 ''' 11 定义加法运算函数 12 :param x: 数字x 13 :param y: 数字y 14 :return: 返回 x 和 y的和 15 ''' 16 return x + y 17 def subtraction(x,y): 18 return x - y 19 def multiplication(x,y): 20 return x * y 21 def division(x,y): 22 return x / y 23 if __name__ == '__main__': 24 print('欢迎来到简易版计算器:') 25 print('1.加法运算') 26 print('2.减法运算') 27 print('3.乘法运算') 28 print('4.除法运算') 29 while True: 30 type = int(input('请输入你要运算的类型:')) 31 if type == 1: 32 a,b = in_put() 33 print(addition(a,b)) 34 if type == 2: 35 a,b = in_put() 36 print(subtraction(a,b)) 37 if type == 3: 38 a,b = in_put() 39 print(multiplication(a,b)) 40 if type == 4: 41 a,b = in_put() 42 print(division(a,b))
- 方法二
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def jsq(): 2 while True: 3 one = float(input("请输入第一个数:")) 4 # 定义符号 5 sign = input("请输入符号:") 6 # 第二个数 7 two = float(input("请输入第二个数:")) 8 # 定义计算结果 9 if sign == "+": 10 print("{} {} {} = {}".format(one,sign,two,one+two)) 11 elif sign == "-": 12 print("{} {} {} = {}".format(one,sign,two,one-two)) 13 elif sign == "*": 14 print("{} {} {} = {}".format(one,sign,two,one*two)) 15 elif sign == "/": 16 print("{} {} {} = {}".format(one,sign,two,one/two)) 17 else: 18 print("请重新输入") 19 jsq()
作业四:2.百文百鸡 while循环 for ... in ...公鸡 2钱一只 母鸡 1钱一只 小鸡 0.5钱一只 共 100钱公鸡最多50只,母鸡最多100只,小鸡最多100只 ,要求算出一共有多少种买法,所有买法打印出来
- 方法一
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # 定义一个函数 2 def by_hundred_chickens_one(): 3 ''' 4 用while循环写的百文百鸡问题 5 :return: 6 ''' 7 # 定义公鸡为 cock 8 cock = 0 9 # 记录总共有多少种方法 10 count=0 11 # 循环,公鸡个数最多50 12 while cock <= 50: 13 # 定义母鸡为hen 14 hen = 0 15 # 循环,母鸡个数最多100 16 while hen <= 100: 17 # 定义小鸡为 chick 18 chick = 0 19 # 循环,小鸡个数最多200 20 while chick <=100: 21 # 判断总钱数,和总个数 22 if cock*2 + hen + 0.5*chick ==100 and cock + hen + chick == 100: 23 print(f"公鸡个数:{cock},母鸡个数:{hen},小鸡个数:{chick}") 24 # 符合条件,count自增1 25 count +=1 26 chick += 1 27 hen += 1 28 cock += 1 29 print(f"共有{count}种方法") 30 # 调用函数 31 by_hundred_chickens_one()
- 方法二
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # # 定义函数 2 def by_hundred_chickens_two(): 3 ''' 4 用for循环写的百文百鸡问题 5 :return:返回方法总数 6 ''' 7 # 记录方法总数 8 count = 0 9 # 定义公鸡cock循环50次 10 for cock in range(0,51): 11 # 定义母鸡chen循环100次 12 for hen in range(0,101): 13 # 定义小鸡chick循环100次 14 for chick in range(0,101): 15 # 判断总钱数,和总个数 16 if cock*2 + hen + chick*0.5 == 100 and cock + hen + chick == 100: 17 print("公鸡个数:{},母鸡个数:{},小鸡个数:{}".format(cock,hen,chick)) 18 # 符合条件,count自增1 19 count += 1 20 # 返回 count(个数) 21 return count 22 # 调用函数,并且打印函数返回值 23 print(f"共有 {by_hundred_chickens_two()} 种方法")