1、使用while循环输出1 2 3 4 5 6 8 9 10
count = 0
while count < 10:
count += 1
if count == 7:
print(end=‘ ‘)
continue
print(count, end=‘ ‘)
1 2 3 4 5 6 8 9 10
//////////////////////////////////////////////////////////////
2、求1-100的所有数的和
sum1 = 0
count = 0
while count < 100:
count += 1
sum1 = sum1 + count
print(f"所有数的和为:{sum1}")
所有数的和为:5050
///////////////////////////////////////////////////////////////
3、输出 1-100 内的所有奇数
for i in range(1,101,2):
print(i)
4、输出 1-100 内的所有偶数
for i in range(2,101,2):
print(i)
5、求1-2+3-4+5 ... 99的所有数的和
sum1 = 0
count = 0
while count < 99:
count += 1
if count % 2 == 0:
sum1 = sum1 - count
else:
sum1 = sum1 + count
print(f"所有数的和为:{sum1}")
所有数的和为:50
6、猜年龄游戏
```python
要求:
1、允许用户最多尝试3次
2、每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
3、如果猜对了,就直接退出
```
db_age = ‘68‘
tag = True
count = 0
while tag:
age = input(‘请猜数字:‘)
if age == db_age:
print(‘恭喜您猜对了!‘)
tag = False
else:
print(‘猜错了!‘)
count += 1
if count == 3:
answer = input(‘您还想继续玩么,请回答Y或N:‘)
if answer.upper() == ‘Y‘:
count = 0
elif answer.upper() == ‘N‘:
tag = False
else:
print(‘无效指令,不遵守游戏规则,游戏结束!‘)
tag =False
7、运用所学知识,打印以下图案:
```python
*
***
*****
*******
*********
```
max_level = 5
for current_level in range(1, max_level + 1):
for i in range(max_level - current_level):
print(‘ ‘, end="")
for j in range(2 * current_level - 1):
print("*", end="")
print()
```python
*********
*******
*****
***
*
```
max_level = 5
for current_level in range(1, max_level + 1):
for i in range(current_level - 1):
print(" ", end="")
for j in range(11 - 2 * current_level):
print("*", end="")
print()
原文:https://www.cnblogs.com/godlover/p/11794378.html