python基础练习题--For,while 循环

这篇博客通过六个实例介绍了Python的基础应用,包括打印水仙花数、组合数、列表分割、冒泡排序以及整数质因数分解。特别讨论了列表分割的两种实现方式,分别使用while和for循环,并探讨了球的反弹问题和其在n次落地后的高度与行程计算。

# 1.打印水仙花数

print('1.打印100-999的水仙花数')
for i in range(100,1000):
    i_100 = i // 100
    i_10 = (i % 100) // 10
    i_1 = i % 10
    if i == i_1**3 + i_10**3 + i_100**3:
        print(i)
print('-'*50,'\n\n')

 

# 2. 1、2、3、4 组数

print('2. 1、2、3、4组成不同的三位数')
num = [x*100+y*10+z for x in range(1,5)
       for y in range(1,5)
       for z in range(1,5)
       if x != y != z]
print(' 能组成%d位三位数,分别是:' % len(num))
print(num)
print('-'*50,end='\n\n')

 

# 3.列表分割:后期可以作为分类功能使用


# 比如列表[0,0,0,1,1,2,3,3,3,2,3,3,0,0]
# 分割成[[0,0,0],[1,1],[2],[3,3,3],[2],[3,3],[0,0]]

# 3.1while 循环加try实现,较复杂

print('3. 列表分割')
print('-'*25+'while循环+try实现'+'-'*25)
# 初始列表
ori_list = [0,0,0,1,1,2,3,3,3,2,3,3,0,0,0,0,1,65,48,48,9]
# 构建空列表
res_list = []
# 初始化序列值 i
i = 0
while i < len(ori_list):
    try:
        if ori_list[i] != ori_list[i+1]: # 如果i 和 i+1 不等,就切,否则继续循环
            res_list.append(ori_list[:(i+1)])
            ori_list = ori_list[(i+1):]
            i = 0
        else:
            i += 1
    except Exception:   # 会出现超过序列索引的异常
        if len(ori_list) == 1:  # 最后剩下的元素只剩一个
            res_list.append(ori_list)
            break
        else:
            if ori_list[0] == ori_list[1]: # 剩下两个相等元素
                res_list.append(ori_list)
                break
            else:   # 剩下两个不等元素
                res_list.append(ori_list[:1])
                res_list.append(ori_list[-1:])
                break
print(res_list)
print('-'*50,end='\n\n')

# 3.2 for循环'实现

print('-'*25+'for循环实现'+'-'*25)
# 试验发现:for循环不能重置 i 的初始值,所以不能用 i 做索引值,需要添加新变量Start作为索引值
list02 = [0,0,0,1,1,2,3,3,3,2,3,3,0,0,0,0]
sp_list02 = []
# 初始化切片开头索引值
start = 0
for i in range(len(list02)-1):
    if list02[i] != list02[i+1]:
        sp_list02.append(list02[start : i+1])
        start = i + 1
sp_list02.append(list02[start:])
print(sp_list02)
print('-'*50,end='\n\n')

 

# 4.冒泡排序



# 4.1由小到大

list1= [12,14,5,9,30,16]
res = [5, 9, 12, 14, 16, 30]
# 由小到大
print('由小到大排列list1')
for i in range(len(list1)-1):
    for j in range(1,len(list1)):
        if list1[j-1] > list1[j]:
            list1[j],list1[j-1] = list1[j-1],list1[j]
print(list1)


# 4.2由大到小

print('由大到小排列res')
for i in range(len(res)-1):
    for j in range(1,len(res)):
        if res[j-1] < res[j]:
            res[j],res[j-1] = res[j-1],res[j]
print(res)
print('-'*50,end='\n\n')

# 5.将一个整数分解质因数。例如: 输入90  打印出2*3*3*5=90

print('5. 分解质因数')
n_list = []
n = int(input('请输入要分解的因数:'))
for i in range(2,n+1):
    while True:
        if n % i == 0:
            n_list.append(i)
            n /= i
        else:
            break
print(n_list)

# 6.球的反弹问题

问题:给定一个初始高度,求反弹n次之后,球距地高度,和第n次落地时,球走过的距离

# 求球反弹高度
a1 = int(input('球的初始高度:'))
c = int(input('反弹次数:'))
i = 1
if c == 1:
    d = a1
    an = 0.5 * a1
else:
    d = a1
    an = 0.5 * a1
    while i<c:
        d += 2*an
        an = 0.5*an
        i+=1
print('第%d次落地时,共经过%d米' % (c,d))
print('第%d次反弹高度%f米' % (c, an))

 

以下是一些Pythonwhile循环基础练习题: 1. **输出1 - 10**:使用while循环输出1 2 3 4 5 6 8 9 10。 ```python count = 0 while count < 10: count += 1 print(count) ``` 2. **计算1 - 100的**:求1 - 100的所有数的。 ```python count = 0 total = 0 while count <= 100: total += count count = count + 1 print(total) ``` 3. **输出1 - 100内的奇数**:输出 1 - 100 内的所有奇数。 ```python count = 1 while count <= 100: if count % 2 != 0: print(count) count += 1 ``` 4. **输出1 - 100内的偶数**:输出 1 - 100 内的所有偶数。 ```python count = 1 while count <= 100: if count % 2 != 1: print(count) count += 1 ``` 5. **用户登陆(三次机会重试)**:用户有三次机会输入用户名密码进行登陆验证。 ```python count = 0 while count < 3: name = input("请输入用户名") password = input("请输入密码") if name == "huxiaohui" and password == "123456": print("你答对啦") break else: print("错啦 再来一次") count += 1 ``` 6. **计算1 - 10的**:计算1 - 10的整数之。 ```python i = 1 sum = 0 while i < 10: sum += i i += 1 print(sum) ``` 7. **猜数字**:随机生成一个1 - 10的数字,用户进行猜测,直到猜对为止,并记录猜测次数。 ```python import random num = random.randint(1, 10) count = 1 result = int(input("请输入你猜的数字")) while result != num: count += 1 if result > num: print("数字大了") else: print("数字小了") result = int(input("请重新输入你猜的数字")) print(f"你总共猜测了{count}次") ``` 8. **输出九九乘法表**:使用while循环输出九九乘法表。 ```python i = 1 while i <= 9: j = 1 while j <= i: print(f'{j}*{i}={i*j}', end='\t') j += 1 print() i += 1 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值