一日精通python(三)

#选择与判断
#1、条件语句,大体上和C语言差不多,把多个条件联系起来,and or nor
#if语句,注意input 输入的是字符串.
#同时需要注意的是缩进
userinput=input("enter 1 or 2")
if userinput=="1":
    print("hello world")
    print("how are you?")
elif userinput=="2":
        print("python roket!")
        print("I LOVE python")
else:
            print('''
feind
ujds
''')

#内联if
mylist=10
print("this is task a" if mylist==10 else "this is task b")

#for循环
pets=["cats","dogs","rabbits","hamster"]
for mypets in pets:
    print(mypets)

#显示列表中的成员的索引值,使用enumerate()
for index,mypets in enumerate(pets):
    print(index,mypets)

#如何在字符串上循环
strings="hello"
for i in strings:
    print(i)
#输出
'''  enter 1 or 21
hello world
how are you?
this is task a
cats
dogs
rabbits
hamster
0 cats
1 dogs
2 rabbits
3 hamster
h
e
l
l
o
'''

#在一段数字上循环
for i in range(5):
    print(i)
    '''输出
0
1
2
3
4

这里和切片由相似之处,都不包括最后的'''
for i in range(1,5,2):
    print(i)
#前两位是起始,第三位等同于步进
'''输出
1
3
'''

#while循环
counter=5
while counter>0:
    print("counter=",counter)
    counter-=1
'''输出
counter= 5
counter= 4
counter= 3
counter= 2
counter= 1
'''

#break中断
j=0
for i in range(5):
    j+=2
    print("i=",i,",j=",j)
    if j==6:
        break
'''输出
i= 0 ,j= 2
i= 1 ,j= 4
i= 2 ,j= 6
'''

#continue
#然后学习try和except,这个语句在错误发生时可以控制程序执行的方式
try:
    user1=int(input("a number"))
    user2=int(input("a number"))
    answer=user1/user2
    print("anwser is",answer)
except ValueError:
    print("you  not input a number")
except ZeroDivisionError:
    print("you input a zero")
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值