# python程序流程控制练习题

XAMPP案例 2291浏览

``````range(stop)
``````range(start, stop[, step])
``````# 参数说明
``````# start :计数开始的地方。默认是从0开始，例如。range(5)等价于range(0,5)
``````# stop : 计数结束的地方，但不包括stop,即左闭右开。例如，range(0,5),返回一个包含0、1、2、3、4的迭代器，而不包括5
````# step : 即步长，默认为1。例如，range(0,5)等价于range(0,5,1)````

示例如下：

``````for i in range(5)
``````    print(i)
``````#输出 0
``````#     1
``````#     2
``````#     4
``````list(range(5))
``````# 输出 [0, 1, 2, 3, 4]
``````list(range(0))
``````# 输出 []
``````list(range(0, 10, 2))
``````# 输出 [0, 2, 4, 6, 8]
``````list((range(10, 0, -1))
``````# 输出 [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
``````list(range（1, 0))
````# 输出 []````

01

``````# -*- coding:utf-8 -*-
``````res = 0
``````for i in range(1,101):
``````    res = res+i
````print(res)````
02—

``````# -*- coding:utf-8 -*-
``````res = 0
``````for i in range(10,0,-1):
````    res = res + i``print(res)````
03

``````# -*- coding:utf-8 -*-
``````res = 0
``````i = 2
``````while i<=100:
``````    res = res + i
``````    print(i)
``````    i = i + 2
````print(res)````
04编写程序，计算 ，项数n由输入控制。
``````# -*- coding:utf-8 -*-
``````n = int(input("请输入项数："))
``````res = 0
``````for i in range(1,n+1):
``````    res = res + 1.0/i
````print(res)````
05

``````# -*- coding:utf-8 -*-
``````# 上三角
``````print("九九乘法表之上三角")
``````for i in range(1,10):
``````    for j in range(1,10):
``````        if i<=j:
``````            print(str.format("{0}×{1}={2:2}",i,j,i*j),end= "     ")
``````    print(" ")
``````print("-"*100)
``````# 下三角
``````print("九九乘法表之下三角")
``````for i in range(1,10):
``````    for j in range(1,10):
``````        if i>=j:
``````            print(str.format("{0}×{1}={2:2}",i,j,i*j),end= "     ")
``````    print("  ")
``````print("-"*100)
``````# 矩形
``````print("九九乘法表之矩阵")
``````for i in range(1,10):
``````    for j in range(1,10):
``````        print(str.format("{0}×{1}={2:2}",i,j,i*j),end= "     ")
````    print("  ")````
06

（2）已知三角形的三条边，则三角形的面积 ，其中h为三角形周长的一半。

``````# -*- coding:utf-8 -*-
``````import math
``````a=eval(input("请输入三角形的边A："))
``````b=eval(input("请输入三角形的边B："))
``````c=eval(input("请输入三角形的边C："))
``````print(str.format("三角形三边分别为：a={0:2.1f},b={1:2.1f},c={2:2.1f}。",a,b,c))
``````if a+b>c and a+c>b and b+c>a  and a>0 and b>0 and c>0:
``````    l=(a+b+c)/2
``````    area=math.sqrt(l*(l-a)*(l-b)*(l-c))
``````    zhouchang = a+b+c
``````    print(str.format("该三角形的周长为={0:2.1f},面积={1:2.1f}。",zhouchang,area))
``````else:
````    print("无法构成三角形")````
07

（2）一般地，累乘的初值为1，而累加的初值为0。

（3）如果输入的是负整数，则继续提示输入非负整数，直到n>=0。

``````# -*- coding:utf-8 -*-
``````while True:
``````    n=int(input("请输入非负整数n:"))
``````    if n<0:
``````        continue
``````    elif n==0:
``````        print("  for循环：0！= 1")
``````        print("while循环：0！= 1")
``````    else:
``````        #for 循环
``````        res1 = 1
``````        for i in range(n,0,-1):
``````            res1 = res1 * i
``````        print(str.format("  for循环：{0}！= {1}",n,res1))
``````        #while 循环
``````        res2 = 1
``````        temp = n
``````        while temp:
``````            res2 = res2*temp
``````            temp = temp - 1
``````        print(str.format("while循环：{0}！= {1}", n, res2))
````        break````
08

``````# -*- coding:utf-8 -*-
``````sum1=sum2=0
``````for i in range(10):
``````    num=eval(input("请输入一个整数："))
``````    if num%2==1:
``````        sum1=sum1+num
``````    else:
``````        sum2=sum2+num
``````avg=(sum1+sum2)/10
``````print("所有奇数之和：{}，所有偶数之和：{}，平均值:{}".format(sum1,sum2,avg))
``````
``````
09

``````# -*- coding:utf-8 -*-
``````wuli=1
``````for i in range(1,366):
``````    if i%6==0 or i%7==0:
``````        wuli=wuliz*(1-0.01)
``````    else:
``````        wuli=wuli*(1+0.01)
````print("郭大侠的最终武力值为:{:.2f}".format(wuli))````
10

1个1个地取，正好取完；

2个2个地取，还剩1个；

3个3个地取，正好取完；

4个4个地取，还剩1个；

5个5个地取，还剩1个；

6个6个地取，还剩3个；

7个7个地取，正好取完；

8个8个地取，还剩1个；

9个9个地取，正好取完。请问：这个盒子中至少有多少个糖果？

``````# -*- coding:utf-8 -*-
``````n=1
``````while 1:
``````    if n%2==1 and n%3==0 and n%4==1 and n%5==4 and n%6==3 and n%7==0 and n%8==1 and n%9==0:
``````        print("这个盒子里一共有{}个糖果".format(n))
``````        break
``````    else:
````        n=n+1````