1.对于判断语句if—else—elif、循环语句for—while等,在python中,不像c++一样需要用{ }去包含子集;在python中使用冒号:开始,结合缩进符来包含子集
2.在c++中,条件判断用()来包含;在python中取消了条件判断的括号直接在关键字之后 加一个空格符再添加判断语句
3.c++中的bool值为true,python中True中的T应该大写
4.与c++的for循环不同的是,python用range(起始值,阈值,自增量)来限制循环次数
while循环
oldBoy=50
i=0
#----------1 在python中,不像c++一样需要用{}去包含子集;在python中使用冒号:开始,结合缩进符来包含子集-------
#----------2 在c++中,条件判断用()来包含;在python中取消了条件判断的括号直接在关键字之后空格符后 开始
#while True:#------3 c++中的bool值为true,python中True中的T应该大写
while i<3:
age=int(input("input the oldBoyAge:"))#input默认输入格式为字符串类型str,所以需要强制转换成int类型
if age==oldBoy:
print("you get the correct age")
break
elif age>oldBoy:#------4 与c++不同的是,c++中的判断语句else if在python中直接变成了elif
print("think smaller")
else:
print("think bigger")
i=i+1
if i==2:
print("do you want to go on?")
select=input("your thinking:")
if select=="y":
print("***************")
i=0
else:
print("---------------")
break#跳出当前循环(如果由多层循环,则只跳出当前层次的循环,更外层的循环继续进行。若需跳出外层循环,则可以尝试设置flag,然后判断)。continue则是跳出此次循环,继续下一次循环
for循环
oldBoy=50
i=0
for i in range(0,3,1):#与c++的for循环不同的是python用range(起始值,阈值,自增量)来限制循环次数 c++则需要这样写:for(int i=0;i<3;i++)
age=int(input("input the oldBoyAge:"))
if age==oldBoy:
print("you get the correct age")
break
elif age>oldBoy:
print("think smaller")
else:
print("think bigger")