Python程序设计基础——循环语句

复习:>>> print(format(987654.129,',.2f'))987,654.13//格式化要四舍五入>>> print("one",end = " ")// 抑制print函数的自动换行one >>> print("two", end= "l")twol>>> print('one','two','three', sep = '~~')//插入间隔符one~~two~~three循环结构:1。条件
摘要由CSDN通过智能技术生成

复习:

>>> print(format(987654.129,',.2f'))
987,654.13//格式化要四舍五入
>>> print("one",end = " ")// 抑制print函数的自动换行
one 
>>> print("two", end= "l")
twol
>>> print('one','two','three', sep = '~~')//插入间隔符
one~~two~~three

循环结构:
1。条件控制的循环——while循环
使用条件:只知道程序执行的条件,不知道程序执行的次数
基本格式:


>>> while condition:
	statement
	statement
	etc.

特点:

1.只要布尔条件为真,循环就会不断执行语句,造成无限循环。
2.condition后面要加冒号。
3.语句要向内缩进。
4. while循环时先测试的循环。所以必须在循环之前采取某些措施让while最少循环一次。

无限循环
while循环就像一根绳子,必须要首位相接才能形成一个闭环的圆圈,例如:

Max_temp= 102.5

temperature = float(input("Enter the substance's Celsius temperature: "))

while temperature > Max_temp:
    print("the temperature is too high")
    print("turn the thermostat down and wait")
    print("5 minutes.Take the temperature again and enter it")
    temperature = float(input("Enter the substance's Celsius temperature: "))

print("the temperature is acceptable")
print("Check it again in 15 minutes")

前后总共输入了两次temperature,这样子才能保证不陷入死循环,这样子条件才能更新,从而重新判断条件。也就是说,在循环内部需要编写让测试条件为假的代码。(按下Ctrl+C来中断程序)

for循环:计数控制的循环-----只知道程序执行的次数。
基本格式:

for variable in [value1,value2,etc.]:
    statement
    statement
    etc.

for循环的基本原理:

>>> for num in [1,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值