Python – While 条件的重要性(while循环)

作者 : 慕源网 本文共2317个字,预计阅读时间需要6分钟 发布时间: 2021-10-2 共487人阅读

现在,我们将了解 while 语句的重要性。

开始吧,

声明

  • 我们可以在条件为真时运行该语句,假表示退出。
  • 而语句在项目中起着非常重要的作用。

句法

流程图

while 语句的简单示例

Input:

a=1
while a<=6:
    a=a+1
    print(a)

Output:
2
3
4
5
6
7

打印给定值提到的次数

Input:

count=0
while count<5:
    print(2,end=' ')
    count=count+1

Output:
22222

因此按降序排列的数字

Input:

last=int(input("Enter the last number:"))
while last>0:
    print(last)
    last=last-1

Output:
Enter the last number:5
5
4
3
2
1

2 次跳跃的升序(奇数)

Input:

first=1
last=int(input("Enter the last number:"))
while first<=last:
    print(first)
    first=first+2

Output:
Enter the last number:5
1
3
5

2 次跳跃的升序(偶数)

Input:

first=2
last=int(input("Enter the last number:"))
while first<=last:
    print(first)
    first=first+2

Output:
Enter the last number:9
2
4
6
8

使用 while 循环的 3 的倍数

Input:

first=0
last=20
while first<=last:
    first=first+3
    print(first)

Output:
3
6
9
12
15
18
21

2 次跳跃的 3 的倍数

Input:

first=1
last=50
while first<=last:
    if first%3==0:
        print(first)
    first=first+2

Output:
3
9
15
21
27
33
39
45

使用循环打印 (ix 3) 值

Input:

first=3
while first<=100:
    print(first)
    first=first*3

Output:
3
9
27
81

使用 while 和嵌套 if 可被 2 和 3 整除的数字

Input:

number=1
while number<=70:
    if number%2==0:
        if number%3==0:
            print(number)
    number=number+1

Output:
6
12
18
24
30
36
42
48
54
60
66

使用 while、if 和逻辑运算符可被 2 和 3 整除的数字

Input:

number=1
while number<=80:
    if (number%2==0 and number%3==0):
        print(number)
number=number+1

Output:
6
12
18
24
30
36
42
48

前n个连续数之和

Input:

number=1
last=100
total=0
while number<=100:
    total=total+number
    number=number+1
print(total)

Output:
5050

而其他使用循环条件

Input:

name='mark zuckerberg'
person=''
while person!=name:
        person=input("Please tell me your name:")
else:
        print("I got your name")

Output:
Please tell me your name:larry page
Please tell me your name:zuckerberg
Please tell me your name:mark
Please tell me your name:mark zucker
Please tell me your name:mark zuckerberG
Please tell me your name:Mark ZuckeRbeRg
Please tell me your name:mark zuckerberg
I got your name

使用索引打印单词

Input:

language ='Python'
i=0
while i<len(language):
        print(language[i])
        i=i+1

Output:
P
y
t
h
o
n

使用while循环语句的游戏

Input:

mind = 7
match_not_found = True
while match_not_found == True:
       guess =  int(input("\nGuess my memory number between 1 & 20 : "))
       if guess == mind:
              print("Your guess is correct")
              match_not_found = False
       elif guess > mind:
              print("Your guess is Higher")
       else:
              print("Your guess is Lower")

Output:
Guess my memory number between 1 & 20 : 15
Your guess is Higher
Guess my memory number between 1 & 20 : 10
Your guess is Higher
Guess my memory number between 1 & 20 : 5
Your guess is Lower
Guess my memory number between 1 & 20 : 6
Your guess is Lower
Guess my memory number between 1 & 20 : 9
Your guess is Higher
Guess my memory number between 1 & 20 : 7
Your guess is correct

我希望现在你能理解 While 循环。如果您有任何疑问,请询问我。以后我们会看到更有趣的话题。

 


慕源网 » Python – While 条件的重要性(while循环)

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录