Python入门-循环实例操作及跳出循环

一、计算阶乘:1*2*3*4*...*n

for循环计算阶乘的方式:

#forfact.py
n=int(input('Enter an integer>=0:'))
fact=1
for i in range(2,n+1):
    fact=fact*i
print(str(n)+' factorial is '+ str(fact))
结果是:

>>> 
Enter an integer>=0:10
10 factorial is 3628800

while循环计算阶乘的方式:

#whilefact.py
n=int(input('Enter an integr >=0: '))
fact=1
i=2
while i<=n:
  fact=fact*i
  i=i+1
print(str(n)+' factorial is '+str(fact))
结果是:

>>> 
Enter an integr >=0: 10
10 factorial is 3628800

二、计算用户输入的数字的总和

下面程序是让用户输入一些数字,然后打印出这些数字的总和,下面是for循环的案例:

#forsum.py
n=int(input('How many numbers to sum?'))
total=0
for i in range(n):
    s=input('Enter number '+str(i+1)+': ')
    total=total+int(s)
print('The sum is '+str(total))
结果是:

>>> 
How many numbers to sum?4
Enter number 1: 24
Enter number 2: 22
Enter number 3: 46
Enter number 4: 56
The sum is 148

再次测试一下while循环:

#whilesum.py
n=int(input('How many numbers to sum? '))
total=0
i=1
while i<=n:
    s=input('Enter number '+str(i)+': ')
    total=total+int(s)
    i=i+1
print('The sum is '+str(total))
结果如下:

>>> 
How many numbers to sum? 3
Enter number 1: 49
Enter number 2: 89
Enter number 3: 26
The sum is 164

三、计算未知个数字的总和

假设输入一系列数字,用户输入'done'的时候就对相应前面输入的数字进行求和,程序是:

#donesum.py
total=0
s=input('Enter a number (or "done"): ')
while s!='done':
    num=int(s)
    total=total+num
    s=input('Enter a number (or "done"): ')
print('The sum is '+str(total))
结果如下:

>>> 
Enter a number (or "done"): 23
Enter a number (or "done"): 45
Enter a number (or "done"): 78
Enter a number (or "done"): done
The sum is 146

该程序基本思想是:不断要求用户输入数字,直到用户输入'done'才罢手。该程序预先不知道循环体将执行多少次。

注意:该程序必须在两个地方调用input:循环体前面和循环体内,之所以这样操作,使因为循环条件判断输入的是数字还是'done'.

不需要使用到计算器变量i。在前面计算总和的程序中,i用于记录用户输入多少数字,但此处不需要。

四、跳出循环和语句块

break语句能够从循环体内的任何地方跳出循环,如:

#donesum_break.py
total=0
while True:
    s=input('Enter a number (or "done"): ')
    if s=='done':
        break # jump out of the loop
    num=int(s)
    total=total+num
print('The sum is '+str(total))
结果是:

>>> 
Enter a number (or "done"): 35
Enter a number (or "done"): 38
Enter a number (or "done"): 98
Enter a number (or "done"): 90
Enter a number (or "done"): 78
Enter a number (or "done"): 27
Enter a number (or "done"): done
The sum is 366

while循环条件为True的时候,将永远循环下去,直到break被执行。

仅当s为done的时候,break才被执行。

一般而言,除非break语句让代码更简单或更清晰,否则不要使用它。

一个与break语句相关的是continue语句:在循环体中调用continue语句时候,将立即转到循环条件,即进入下一个迭代,但编程过程中,尽量避免使用continue语句。







### Python 初学者示例教程 对于希望学习 Python 编程基础概念和语法的新手来说,官方文档提供了丰富的入门资料[^1]。下面是一些基本的例子来帮助理解 Python 的核心特性。 #### 打印 "Hello, World!" 这是最经典的编程练习之一: ```python print("Hello, World!") ``` 这段简单的代码展示了如何使用 `print()` 函输出文本到控制台。 #### 变量与据类型 Python 支持多种内置的据类型,包括整、浮点、字符串等: ```python integer_example = 42 # 整型变量 float_example = 3.14 # 浮点型变量 string_example = 'Python' # 字符串变量 print(integer_example) print(float_example) print(string_example) ``` #### 条件语句 条件逻辑允许程序根据不同的情况执行不同分支的代码: ```python number = 10 if number > 5: print(f"{number} 大于 5") # 当条件为真时运行此行 else: print(f"{number} 小于等于 5") # 否则运行这行 ``` #### 循环结构 循环用于重复执行一段代码直到满足特定条件为止: ```python for i in range(5): # 使用 for 循环迭代序列中的项 print(i) while True: # while 循环会一直运行直至内部 break 或者外部干预停止它 user_input = input('输入 q 退出:') if user_input.lower() == 'q': break # 如果用户输入的是 'q', 跳出循环并结束程序 ``` 除了上述例子外,在线还有许多其他优秀的免费资源可以辅助学习 Python 编程[^2]。这些材料通常覆盖更广泛的主题和技术细节,适合不同程度的学习者深入探索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值