python中的循环语句-01
接触python已经一周时间,下面针对这一周python学习的情况做一梳理:
1)单行注释,使用一个#;多行注释,使用三个单引号(或者三个双引号)如:
name = input("name:") age = int(input("age:")) # 输入学员的名字 '''print(type(age)) job = input("job:") salary = input("salary:")'''
2)条件分支
if guess_age == age_of_oldboy: print("yes,you have got it!") break elif guess_age < age_of_oldboy: print("no,you should think bigger") else: print("no,you should think smaller!")
python一个比较好的点是,当if语句写好后,冒号,回车,下一行会自动缩进。
3)while条件
例:
while count< 3: guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("yes,you have got it!") break elif guess_age > age_of_oldboy: print("no,you should think smaller!") else: print("no,you should think bigger!") count = count + 1 if count ==3: countine_confirm = input("do you want to keep guessing..?") if countine_confirm != 'n': count =0
break:终止当前的循环体。跳到外层程序;
continue:终止本轮循环,开始下一轮循环;
4)当没有任何类型指定时,pycharm默认输入的是字符串。
例:
age = input(“age of oldboy:”)
print("type of age:",type(age))
屏幕上会让用户输入age值,
age of oldboy:56 type of age: <class 'str'>
显示age的类型为str。
当限定age类型为int类型时:
age = int(input("age of oldboy:")) print("type of age:",type(age))
结果显示如下:
age of oldboy:34 type of age: <class 'int'>
age的类型为int整型了。所以默认的是字符串型,当需要时要指定类型。