while循环 II#
Ex0#
指出下面程序的bug:
def test():
counter = 0;
while counter > 100
if (counter % 2 == 1):
cout << counter << " is odd." << endl;
else
cout << counter << " is odd." << endl;
count = count + 1
#test()
File "/tmp/ipykernel_608/1427942845.py", line 3
while counter > 100
^
SyntaxError: invalid syntax
while循环又称为”当型循环”:当条件为真时,循环执行某段程序。
我们写while循环的时候,确定循环条件是最关键的步骤之一。
Ex1#
下面程序的循环条件是什么?最后打印的值代表什么含义?
i = 1
sum = 0
while(sum<1000):
i=i+1
sum = sum + i
print(i-1)
Ex2#
下列程序的循环条件是什么?最后的count代表什么含义?
def f(str):
count = 0
i = 0
while(i < len(str)):
if(str[i] == 'a'):
count = count+1
i = i+1
return count
Ex3#
Print the square roots of the first 25 odd positive integers.
import math
def roots():
#add your code here
roots()
Ex4#
2019年年末中国人均GDP为10276美金,也是首次超过10000美金。一般来说,如果人均GDP超过2万美金,则说明该国家称为了中等发达国家。
假设中国人均GDP年平均增长率为3.5%,请问中国成为中等发达国家的时间是?
def calculate():
year = 2019
gdp = 10276
while():
print(calculate())
课件#
课件下载:Loops II