python3的自学之路--2_python的循环语句

1、使用while循环输入 1 2 3 4 5 6 8 9 10
 

count = 0
while count < 10:
    count += 1  # count = count + 1
    if count == 7:
        print(' ')
    else:
        print(count)

count = 0
while count < 10:
    count += 1  # count = count + 1
    if count == 7:
        continue
    print(count)

2.输出 1-100 内的所有奇数

#方法一:
 count = 1
while count < 101:
     print(count)
     count += 2
#方法二:
 count = 1
 while count < 101:
     if count % 2 == 1:
         print(count)
     count += 1

3.求1-2+3-4+5 ... 99的所有数的和

 sum = 0
 count = 1
 while count < 100:
     if count % 2 == 0:
         sum = sum - count
     else:
         sum = sum + count
     count += 1
 print(sum)

4.用户登陆(三次机会重试)
input 心中有账号,密码 while

i = 0
while i < 3:
    username = input('请输入账号:')
    password = int(input('请输入密码:'))
    if username == '123' and password == 123:
        print('登录成功')
    else:
        print('登录失败请重新登录')
    i += 1



count = 0
while count <= 5 :
    count += 1
    if count == 3:break
    print("Loop",count)

else:
    print("循环正常执行完啦")
print("-----out of while loop ------")

5.格式化输出


 name = input('请输入姓名')
 age = input('请输入年龄')
 height = input('请输入身高')
 msg = "我叫%s,今年%s 身高 %s" %(name,age,height)
 print(msg)




name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入工作:')
hobbie = input('你的爱好:')

msg = '''------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end -----------------''' %(name,name,int(age),job,hobbie)
print(msg)



name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
print(msg)

6.python中的优先级

#()> not > and > or
# print(2 > 1 and 1 < 4)
# print(2 > 1 and 1 < 4 or 2 < 3 and 9 > 6 or 2 < 4 and 3 < 2)
# T or T or F
#T or F
# print(3>4 or 4<3 and 1==1)  # F
# print(1 < 2 and 3 < 4 or 1>2)  # T
# print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)  # T
# print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)  # F
# print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)  # F
# print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # F


#ps  int  ----> bool   非零转换成bool True   0 转换成bool 是False
# print(bool(2))
# print(bool(-2))
# print(bool(0))
# #bool --->int
# print(int(True))   # 1
# print(int(False))  # 0

'''x or y x True,则返回x'''
# print(1 or 2)  # 1
# print(3 or 2)  # 3
# print(0 or 2)  # 2
# print(0 or 100)  # 100


# print(2 or 100 or 3 or 4)  # 2

# print(0 or 4 and 3 or 2)
'''x and y x True,则返回y'''
# print(1 and 2)
# print(0 and 2)
print(2 or 1 < 3)
print(3 > 1 or 2 and 2)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值