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整型了。所以默认的是字符串型,当需要时要指定类型。

 

转载于:https://www.cnblogs.com/zhaolu/p/11083901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值