02 while循环 bool值计算
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : day02_homework.py
# @Author: jhchena
# @Date : 2024/3/4
# @Desc :
# @Contact : chenjinhua053@126.com
# 猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。
# 实现功能1
"""luck_number = 66
while True:
guass_number = int(input("请输入你猜测的数字:"))
if guass_number > luck_number:
print("您猜测的结果大了")
elif guass_number < luck_number:
print("您猜测的结果小了")
else:
print("恭喜您猜对了")
break"""
# 在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘大笨蛋’。
# 实现功能2
"""luck_number = 66
count = 1
while True:
guass_number = int(input("请输入你猜测的数字:"))
if guass_number > luck_number:
print("您猜测的结果大了")
elif guass_number < luck_number:
print("您猜测的结果小了")
else:
print("恭喜您猜对了")
break
if count ==3:
print('大笨蛋')
break
count += 1"""
# 使用两种方法实现输出 1 2 3 4 5 6 8 9 10 。
# 方式1:while循环
"""i = 1
while i <= 10:
print(i)
i += 1
"""
# 方式2:for 循环
"""for i in range(11):
if i == 0:
continue
print(i)
i += 1
"""
# 求1-100的所有数的和
"""i = 1
count = 0
while i <= 100:
count = count +i
i += 1
print(count)
"""
# 输出 1-100 内的所有奇数
"""i = 1
count = 0
while i <= 100:
if (i % 2 == 1):
print(i, end=' ')
i += 1"""
"""输出 1-100 内的所有偶数
i = 1
count = 0
while i <= 100:
if (i % 2 == 0):
print(i, end=' ')
i += 1
求1-2+3-4+5 ... 99的所有数的和
i = 1
count = 0
while i < 100:
if (i % 2 == 1):
count = count + i
else:
count = count - i
i += 1
print(count)"""
# ⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
"""count = 1
while True:
user = input("请输入登录的用户名:")
pwd = input("请输入登录用户名密码:")
if user == 'test' and pwd == 'test':
print(f'欢迎{user}登录成功')
break
else:
print("用户名或密码输入错误")
print(f'当前剩余登录次数{3-count}')
if count ==3:
print('登录失败次数已使用完,用户锁定 900s,请稍后重试')
break
count += 1"""
# 简述ASCII、Unicode、utf-8编码
# 简述位和字节的关系?
# 猜年龄游戏
"""luck_age = 66
while True:
guass_age = int(input("请输入你猜测的年龄:"))
if guass_age > luck_age:
print("您猜测的结果大了")
elif guass_age < luck_age:
print("您猜测的结果小了")
else:
print("恭喜您猜对了")
break
"""
# 在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘大笨蛋’。
"""luck_age = 66
count = 1
while True:
guass_age = input("请输入你猜测的年龄:")
if guass_age == luck_age:
print(f'猜测正确')
break
else:
print("猜测错误")
print(f'当前剩余猜测次数{3 - count}')
if count == 3:
print('大笨蛋')
break
count += 1"""
# 猜年龄游戏升级版
# 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出。
"""luck_age = 66
count = 1
while True:
guass_age = input("请输入你猜测的年龄:")
if guass_age == luck_age:
print(f'猜测正确')
break
else:
print(f'当前剩余猜测次数{3 - count}')
if count == 3:
again = input("输入:Y 继续玩,N 退出猜测年龄游戏!! :")
if again == 'Y' or again == 'y':
count = 1
continue
else:
break
count += 1
"""
# 判断下列逻辑语句的 #True, #False
# 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 # True
# False or True or False and True and True or False
# False or True or True or False
# True
print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 # not 最后计算 False
# not True and True or False and True and True or False
# not True or True or False
# not True
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
# 求出下列逻辑语句的值。
# 8 or 3 and 4 or 2 and 0 or 9 and 7 # 8
# 8 or 4 or 0 or 7
# 8 or 0 or 7
# 8 or 7
print(8 or 3 and 4 or 2 and 0 or 9 and 7)
# 0 or 2 and 3 and 4 or 6 and 0 or 3 # 4
# 0 or 4 or 0 or 3
# 4 or 3
print(0 or 2 and 3 and 4 or 6 and 0 or 3)
# 下列结果是什么?
# 6 or 2 > 1 # 6
# 6 or True
print(6 or 2 > 1)
# 3 or 2 > 1 # 3
# 3 or True
# 0 or 5 < 4 # False
# 0 or False
# 5 < 4 or 3 #3
# False or 3
# 2 > 1 or 6 # True
# True or 6
# 3 and 2 > 1 # True
# 3 and True
# 0 and 3 > 1 # True
# 0 and True
# 2 > 1 and 3 # 3
# True and 3
# 3 > 1 and 0 # 0
# True and 0
# 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 # 2
# True and 2 or True and 3 and 4 or True
# 2 or 4 or True
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)
分类:
00 python 作业
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)