while 循环 格式化 逻辑运算

#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)
'''
#3、输出 1-100 内的所有奇数
#方法一:
# count = 1
# while count < 101:
# print(count)
# count += 2
#方法二:
# count = 1
# while count < 101:
# if count % 2 == 1:
# print(count)
# count += 1

#5、求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)

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

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


# 算法 : () > not > and> or 非 或 与 优先级
#
# 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



# int ===> bool 非零转换布尔值是 True 零 就是 false
print(bool(4))
print(bool(-4))
print(bool(0))
# bool===> int 布尔转数字
print(int (True))
print(int (False))
# x or y x 非零,则返回左边x
# print(1 or 2 ) # 1
# print(3 or 2) #3
# print(0 or 3) # 3
# print(0 or 100) # 100

# x and y 和 or 相反

print(1 and 2 ) # 2
print(3 and 2) #2
print(0 and 3) # 0
print(0 and 100) # 0
#
# # 练习:
#
# print(2 or 4 or 9 or 100 or 6) # 2
#
# print( 0 or 5 and 4 or 5 ) # 优先级 先 5 and4 返回4 在 0 or 4 or 5 输出4
#
# print(1>2 and 3 or 4 and 3<2) # *****
#
#
#
# # 用户交互
# name=input('请输入你的名字==》')
# age=input ('请输入你的年龄==》')
# print('我 的名字是'+name,'我的年龄是' +age ) # 语法要求带个加号 + 字符拼接上

if判断
print(111)
if True:
print(222)
print(4444)

if 3>2:
print('玩')
else:
print('不玩')

if 3>4:
print('玩')
else:
print('不玩')

多选
num=input('请输入你猜的数字') # input 输入的是字符串 所以 1 加引号 也可用str()转化
#
# if num==str(1):
# print('吃饭')
# elif num=='2':
# print('玩去')
# elif num=='3':
# print('逛街')
# else:
# print('都猜错了')

while循环

print(222)
while True:
print(444)
print(555)
print(777)

# 无限循环 终止循环 就要改变条件
count=1
flag=True # 标志位
while flag:
print(count) # 算出count = 1
count+=1 # count = count+1 算出结果 2 覆盖掉了 1
if count>100:
flag=False

count= 1
while count<=100:
print(count)
count+=1

count=1
while True:
print(count)
count+=1
if count>10 :break #break 是程序终止


count=1
while count<=10:
print(count)
count+=1

# if count>10 :break #break 是程序终止

# 格式化输出

# name=input('请输入你的名字')
# age=input('请输入你的年龄')
# height=input('请输入你的身高')
# mag=(name,age,height)
# print(mag)




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

mag = '''
name: %s
age: %d
job: %s
hobbie: %s

''' %(name,int(age),job,hobbie)
print(mag)

name = input('请输入姓名')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫%s,今年%s 身高 %s 学习进度为3%%s" %(name,age,height)
print(msg)
 
#⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
# i = 3
# username = "yangxiaoer"
# password = "123456"
# while i>=0:
# name = input("请输入你的用户名:")
# if name == username:
# passwd = input("请输入你的密码:")
# if passwd == password:
# print("登录成功。请稍后")
# print('''
# username: %s
# password: %s
# '''%(username,password))
# break
# else:
# print("你的密码错误 请重新输入")
# print("你还有%s次机会" % (i-1))
# if i == 0:
# print('您的机会已经用完,结束本次操作')
# break
# continue
# else:
# print("你的用户名错误!请重新输入")
# print("你还有%s次机会"%(i-1))
# i -= 1



猜你喜欢

转载自www.cnblogs.com/andy117/p/10927455.html
今日推荐