python中while循环练习题
时间: 2023-10-23 12:09:19 浏览: 196
以下是一些关于Python中while循环的练习题:
1. 使用while循环输出1到10的所有数字。
```python
count = 1
while count <= 10:
print(count)
count += 1
```
2. 求1到100的所有数的和。
```python
count = 1
total = 0
while count <= 100:
total += count
count += 1
print(total)
```
3. 输出1到100内的所有奇数。
```python
count = 1
while count <= 100:
if count % 2 != 0:
print(count)
count += 1
```
4. 输出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
```
阅读全文