python-while循环的练习题# !/use/bin/env python
# -*-conding:utf-8-*-
#author:shanshan
"""
写代码
a. 使⽤ while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12
b. 使⽤while 循环输出100-50,从⼤到⼩,如100,99,98…,到50时再从0循环输出到50,然后结束
c. 使⽤ while 循环实现输出 1-100 内的所有奇数
d. 使⽤ while 循环实现输出 1-100 内的所有偶数
e. 使⽤while循环实现输出2-3+4-5+6…+100 的和
"""
#a. 使⽤ while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12
i = 0
while i <=13:
if i == 6 and i == 10:
break
else:
print(i)
i = i+1
#b. 使⽤while 循环输出100-50,从⼤到⼩,如100,99,98…,到50时再从0循环输出到50,然后结束count = 100
list_100_to_small = []
list_50_to_small = []
while count>0:
if count>=50:
list_100_to_small.append(count)
else:
list_50_to_small.append(count)
#list_50_verse()
count -=1
list_0_big = list_50_to_small[::-1]
list_100_d(list_0_big)
print(list_100_to_small)
#c. 使⽤ while 循环实现输出 1-100 内的所有奇数
count = 1
while count <= 100:
if count % 2 == 1:
print(count)
count +=1
#d. 使⽤ while 循环实现输出 1-100 内的所有偶数
count = 1
while count <= 100:
if count % 2 == 0:
print(count)
count +=1
#e. 使⽤while循环实现输出2-3+4-5+6…+100 的和
count = 2
sum = 0
while count <= 100:
if count % 2 == 1:
sum_count = -count
else:
sum_count = count
sum = sum_count+sum
count +=1
print(sum)
while语句怎么用python

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。