复习:
>>> 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,