180405之循环嵌套

1、使用while循环计算1~100的累积和(包含1和100),但要求跳过所有个位为3的数。

 1 def func1_1(n):
 2     sum = 0
 3     i = 1
 4     while i <= n:
 5         if i % 10 == 3:
 6             sum = sum
 7         else:
 8             sum = sum + i
 9         i += 1
10     print(sum)
def func1(n):
    sum = 0
    for i in range(1,n+1):
        if i % 10 ==3:
            continue
        else:
            sum = sum + i
    print(sum)

2、从键盘获取一个数字,然后计算它的阶乘,例如输入的是3,
那么即计算3!的结果,并输出
提示:
1!等于 1
2!等于 1*2
3!等于 1*2*3
n!等于 1*2*3*...*n

1 def func2():
2     num = int(input("请输入一个数字"))
3     data = 1
4     while num >=1:
5             data  = data*num
6             num = num-1
7     print(data)

3、使用while循环输出如下图形:(必须使用双重while循环实现)
*
* *
* * *
* * * *
* * * * *

def func3():
    i = 1
    while i <= 5:
        j = 1
        while j <=i:
            print('*',end="")
            j +=1
        i += 1
        print()

4、使用while循环输出如下图形:(必须使用双重while循环实现)
*
* *
* * *
* * * *
* * * * *

 1 def func4():
 2     i = 1
 3     while i <=5:
 4         j = 1
 5         while j <=9:
 6             if i%2==1 and (5-i) <j and j< (5+i) and j%2==1:
 7                 print('*',end='')
 8             elif i%2==0 and 5-i <=j<= 5+i and j%2==0:
 9                 print('*',end ="")
10             else:
11                 print(" ",end="")
12             j +=1
13         i +=1
14         print()

5、求 1+2!+3!+...+20! 的和。

 1 def func5():
 2     i = 1
 3     temp = 0
 4     while i <= 20:
 5         data = 1
 6         j = 1
 7         while j <= i:
 8             data = data * j
 9             j = j + 1
10         temp = temp + data
11         i += 1
12     print(temp)

6、本金10000元存入银行,年利率是千分之三。
每过1年,将本金和利息相加作为新的本金。
计算5年后,获得的本金是多少?

1 def func6():
2     year = 1
3     capital = 10000
4     while year <= 5:
5         profit = capital * (3 / 1000)
6         sum_money = capital + profit
7         capital = sum_money
8         year += 1
9     print(capital)

1、题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,
其各个位上数字的立方和等于该数本身。例如:153是一个”水仙花数",
因为153=1的三次方+5的三次方+3的三次方。

 1 import random
 2 
 3 def func1():
 4     for i in range(1,10):
 5         # print(">>",i**3)
 6         for k in range(1,10):
 7             for j in range(1,10):
 8                 a = int("%s%s%s"%(i,j,k))
 9                 if i**3 + k**3 +j**3 == a:
10                     print(a)
11                 else:
12                     pass

2、设计“过7游戏”的程序, 打印出1-100之间除了含7和7的倍数之外的所有数字。

1 def func2(n):
2     for i in range(1,n+1):
3         if i%7==0 or i%10 ==7:
4             print(i)


3、使用while,完成以下图形的输出。(每一行的星星不能用*乘以星星的数量来完成,须使用while嵌套)(较难)

*
***
*****
*******
*********
*******
*****
***
*

 1 def func3():
 2     i = 0
 3     while i < 9:
 4         while i < 5:
 5             j = 0
 6             while j < 9:
 7                 if 4 - i <=j <= 4 + i:
 8                     print("*",end="")
 9                 else:
10                     print(" ",end="")
11                 j += 1
12             print()
13             i +=1
14         while i >= 5 and i< 9:
15             j = 0
16             while j < 9:
17                 if i -4 <=j<= 8 + (4 -i):
18                     print("*",end="")
19                 else:
20                     print(" ",end="")
21                 j += 1
22             print()
23             i +=1

4、使用while、if来完成剪刀石头布程序,要求,
当玩家第3次获胜时才退出游戏,否则继续玩。

5、编写一个程序计算个人所得税(以下为个人所得税税率表,3500元起征):

应纳税所得额(含税) 税率(%)
不超过1500元的 3
超过1500元至4,500元的部分 10
超过4,500元至9,000元的部分 20
超过9,000元至35,000元的部分 25
超过35,000元至55,000元的部分 30
超过55,000元至80,000元的部分 35
超过80,000元的部分 45

 1 def func5():
 2     capital = int(input("请输入你的工资>>>"))
 3     temp = capital - 3500
 4     if temp <= 0:
 5         revenue = 0
 6     elif  0< temp <= 1500:
 7         revenue = temp*0.03
 8     elif 1500<temp<=4500:
 9         revenue = 45 + (temp-1500)*0.1
10     elif 4500<temp<=9000:
11         revenue = 345 + (temp-4500)*0.2
12     elif 9000<temp<=35000:
13         revenue = 1245 + (temp-9000)*0.25
14     elif 35000<temp<=55000:
15         revenue = 7745 + (temp-35000)*0.3
16     elif 55000<temp<=80000:
17         revenue = 13745 + (temp-55000)*0.35
18     else:
19         revenue = 22495 + (temp-80000)*0.45
20     print( revenue )

6、幸运猜猜猜:游戏随机给出一个0~99(包括0和99)的数字,然后让你猜是什么数字。
你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围。
经过几次猜测与提示后,最终推出答案。在游戏过程中,记录你最终猜对时所需要的次数,
游戏结束后公布结果。
说明:
1~2次猜中,打印你太TM有才了!
3~6次猜中,打印这么快就猜出来了,很聪明嘛!
大于7次猜中,打印猜了半天才猜出来,小同志,尚需努力啊!
猜测次数最多20次。(选做)

 1     while account_2 <=10 :
 2         guess = input("请输入数字")
 3         guess = int(guess)
 4 
 5         if guess < num:
 6             print("猜小了")
 7         elif guess > num:
 8             print("猜大了")
 9         else:
10             break
11         account_2 += 1
12 
13     if account_2 <= 2:
14         print("经过%s次猜对,你太TM有才了! " % account_2)
15     elif 2 < account_2 <= 6:
16         print("经过%s次猜对,这么快就猜出来了,很聪明嘛!" % account_2)
17     elif 6 < account_2 <= 10:
18         print("经过%s次猜对了,小同志,尚需努力啊!" % account_2)
19     else:
20         print("太笨了,20次都没有猜对")

7,乘法口诀表

 1 #! /usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # anthor:fhj
 4 
 5 row = 1
 6 while row <= 9:
 7     clum = 1
 8     while clum <= row:
 9         print("%s * %s = %s " % (row, clum, row * clum), end="	")
10         clum += 1
11     print()
12     row += 1
13 
14 row = 9
15 while row >= 1:
16     clum = 1
17     while clum <= row:
18         print("%s * %s = %s " % (row, clum, row * clum), end="	")
19         clum += 1
20     print()
21     row -= 1
原文地址:https://www.cnblogs.com/cerofang/p/8720123.html