python使用while循环打印数字1-7但不要出现4
时间: 2023-05-24 20:03:03 浏览: 102
可以使用如下代码实现:
```
num = 1
while num <= 7:
if num == 4:
num += 1
continue
print(num)
num += 1
```
输出结果为:
```
1
2
3
5
6
7
```
相关问题
Python使用while循环打印
如果你想用 while 循环来打印多行星号,可以像这样写:
```
i = 0
while i < 5:
j = 0
while j < 5:
print("*", end="")
j += 1
print("")
i += 1
```
这段代码会打印出 5 行,每行有 5 个 *。如果你想打印更多的行和更多的星号,可以将外部的 while 循环的判断条件和内部的 while 循环的判断条件替换成你需要的数字。
用不同的循环方式来实现打印1-7,但是不要4。python
好的,以下是三种不同的循环方式来实现这个需求:
1. for循环:
```python
for i in range(1, 8):
if i == 4:
continue
print(i)
```
2. while循环:
```python
i = 1
while i <= 7:
if i == 4:
i += 1
continue
print(i)
i += 1
```
3. do-while循环(Python中没有原生的do-while循环,但是可以通过while True + break来模拟):
```python
i = 1
while True:
if i == 4:
i += 1
continue
print(i)
i += 1
if i > 7:
break
```
以上三种方式都能够实现打印1-7,但不包括数字4的需求。