python之道02
猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了,然后继续让用户输入; 如果比66小,则显示猜测的结果小了,然后继续让用户输入;只有等于66,显示猜测结果正确,然后退出循环。
答案:
num = 66
while True:
my_input = int(input('输入数字: '))
if my_input > num:
print('大了')
elif my_input < num:
print('小了')
else:
print('正确')
break
在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘大笨蛋’
答案:
num = 66
count = 0
while count < 3:
my_input = int(input('输入数字: '))
if my_input > num:
print('大了')
elif my_input < num:
print('小了')
else:
print('正确')
break
count += 1
if count == 3:
print('大笨蛋')
break # 方法二好(小升级) num = 66
count = 0
while count < 3:
my_input = int(input('输入数字: '))
if my_input > num:
print('大了')
elif my_input < num:
print('小了')
else:
print('正确')
break
count += 1
else: # 如果三次之内没有猜测争取,则自动退出并显示'大笨蛋'
print('大笨蛋')
break
判断下列逻辑语句的True,False
- 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 and or
True
False
求出下列逻辑语句的值。
- 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
答案:
8
4
下列结果是什么?
6 or 2 > 1
答案:
63 or 2 > 1
答案:
30 or 5 < 4
答案:
False5 < 4 or 3
答案:
32 > 1 or 6
答案:
True3 and 2 > 1
答案:
True0 and 3 > 1
答案:
02 > 1 and 3
答案:
33 > 1 and 0
答案:
03 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
答案:
2
使用while循环输出 1 2 3 4 5 6 8 9 10 # 这里注意看题没有7
答案:
# 方法一:
count = 0
while count < 11:
if count != 7: # 如果count不等于7就打印
print(count)
count += 1
# 方法二:
count = 1
while count < 11:
if count == 7: # 如果count等于7 就给count加个1再打印,然后count继续+1循环打印
count += 1
print(count)
count += 1
# 方法三:
count = 0
while count < 10:
count += 1
if count == 7:
continue # 如果count等于7, 跳出本次循环,继续下次循环
print(count)
- 求1-100的所有数的和
答案:
x = 1
y = 0
while x < 101:
y = y + x
x += 1
print(y)
- 输出 1-100 内的所有奇数 (奇数就是除以2余数不为0)
答案:
count = 0
while count < 101:
if count % 2 != 0:
print(count)
count +=1
# 个人见解
# 除2余数不为0
- 输出 1-100 内的所有偶数 (偶数就是除以2余数不为1)
答案:
count = 0
while count < 101:
if count % 2 != 1:
print(count)
count +=1
# 除2余数不为1
- 求1-2+3-4+5 ... 99的所有数的和
答案:
x = 1
sum = 0
while x < 100:
if x % 2 == 0:
sum -= x # sum = sum - x
else:
sum += x
x += 1
print(sum)
# 输出结果
50
# 个人见解
# x是从1到99的数,当取值100的时候while循环条件不满足,不再执行.关键条件:当x为偶数也就是取余为0的时候,sum的值就等于
#################################################################
# 原始方法:
count = 1
num_sum = 0 # 偶数的减和
sum_num = 0 # 奇数的加和
while count < 100:
if count % 2 == 0:
num_sum = num_sum - count
else:
sum_num = sum_num + count
count +=1
print(num_sum + sum_num)
######################################################
# 方法二:
count = 1
num_sum = 0 # 总和
while count < 100:
if count % 2 == 0:
num_sum = num_sum - count
else:
num_sum = num_sum + count
count +=1
print(num_sum)
# 规律就是偶数是减,用num_sum = num_sum - count,奇数是加num_sum = num_sum + count
- 简述ASCII、Unicode、utf-8编码英文和中文都是用几个字节?
答案:
ASCII:英文字符 英文1个字节 不支持中文
Unicode:万国码 英文2个字节 中文4个字节
utf-8: 万国码的升级版本 英文1个字节 欧洲2个字节 亚洲3个字节
- 简述位和字节的关系?
答案:
1byte = 8bit
- "老男孩"使用GBK占几个字节,使用Unicode占用几个字节?
答案:
GBK中文2个字节:所以'老男孩'共占6个字节
Unicode中文4个字节:所以'老男孩'共占12个字节
- 猜年龄游戏升级版 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出。
答案:
num = 66
count = 0
while count < 3:
my_input = int(input('输入数字: '))
if my_input > num:
print('大了')
elif my_input < num:
print('小了')
else:
print('正确')
count += 1
if count == 3:
my_input1 = input('用户是否还想继续玩,Y或N: ').strip()
if my_input1.upper() == 'Y':
count = 0
continue
elif my_input1.upper() == 'N':
break
# 方式二:
count = 1
age = 66
while count < 4:
age_input = int(input("请输入年龄:"))
if age_input > age:
print("大了")
elif age_input < age:
print("小了")
else:
print("猜对了")
break
if count == 3:
choice = input("是否继续(Y/N):")
if choice == "Y":
count = 0
else:
break
count += 1
- ⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
答案:
count = 3
while count < 4:
count -= 1
username = input('Name: ').strip()
passwd = input('Password: ').strip()
if username == 'admin' and passwd == '123':
print('成功')
break
else:
print(f'失败,剩余{count}次')
if count == 0:
break
# 老师方法:
count = 3
while count > 0:
user = input("user:")
pwd = input("password:")
if user == 'alex' and pwd == 'alex3714':
print("登录成功!")
break
else:
print(f"你是Alex吗?这都记不住!剩余次数{count - 1}")
count -= 1
python之道02的更多相关文章
- Python补充06 Python之道
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python有一个彩蛋,用下面语句调出: import this 该彩蛋的文档记录 ...
- 彩蛋 Python之道
彩蛋 Python之道 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 使用下面的语句可以调出Python中的一个彩蛋, impo ...
- 【转】Python之道
作者:Vamei 出处:http://www.cnblogs.com/vamei Python有一个彩蛋,用下面语句调出: import this 该彩蛋的文档记录于PEP 20. 语句执行之后,终端 ...
- Python之道(一)之安装Python
"Python之道"首先介绍一下在windows系统下怎样安装Python开发环境. (1)下载MSI安装文件 进入网址www.python.org,点击Downloads进入下载 ...
- Python web前端 02 CSS
Python web前端 02 CSS 一.选择器 1.CSS的几种样式(CSS用来修饰.美化网页的) #建立模板 复制内容--->SETTING---> Editor -----> ...
- 一入python深似海--python之道
python社区不乏幽默.先来看"python之道"这首诗. 导入this包: import this 输出是一首诗,这首诗总结了Python的风格,能够指导Python程序猿的编 ...
- python并发编程02 /多进程、进程的创建、进程PID、join方法、进程对象属性、守护进程
python并发编程02 /多进程.进程的创建.进程PID.join方法.进程对象属性.守护进程 目录 python并发编程02 /多进程.进程的创建.进程PID.join方法.进程对象属性.守护进程 ...
- Python网络编程02 /基于TCP、UDP协议的socket简单的通信、字符串转bytes类型
Python网络编程02 /基于TCP.UDP协议的socket简单的通信.字符串转bytes类型 目录 Python网络编程02 /基于TCP.UDP协议的socket简单的通信.字符串转bytes ...
- Python数学建模-02.数据导入
数据导入是所有数模编程的第一步,比你想象的更重要. 先要学会一种未必最佳,但是通用.安全.简单.好学的方法. 『Python 数学建模 @ Youcans』带你从数模小白成为国赛达人. 1. 数据导入 ...
随机推荐
- wcf中事务的操作
using System; using System.ServiceModel; namespace Larryle.Wcf.ServiceContract.Transaction { [Servic ...
- 整合ssh的时候出现空指针java.lang.NullPointerException
转自:https://blog.csdn.net/koudailidexiaolong/article/details/9468857 HTTP Status 500 - type Exception ...
- jsp实现文件上传(二)用cos组件实现文件上传
jsp表单 <%@ page language="java" pageEncoding="utf-8"%> <html> <hea ...
- UILabel和UIButton添加下划线
关于UILabel和UIButton有的时候需要添加下划线,一般有两种方式通过默认的 NSMutableAttributedString设置,第二种就是在drawRect中画一条下划线,本文就简单的选 ...
- Linux 问题 卸载setup.py方式安装的python包
python ./setup.py install --record install.txt cat install.txt | xargs rm -rf
- MYSQL性能调优与架构设计之select count(*)的思考
select count(*)的思考 原文:MYSQL性能调优与架构设计 举例: 这里我们就拿一个看上去很简单的功能来分析一下. 需求:一个论坛帖子总量的统计 附加要求:实时更新 在很多人看来,这 ...
- hdu3938 Portal 离线+并查集
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int ...
- SpringMVC + ajax
1.ajax 返回汉字乱码 解决方法: http://blog.sina.com.cn/s/blog_5f39177b0101it7h.html //方案一 response.setCharacter ...
- WOW.js 动画使用
有的页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意.比如刚刚发布的 iPhone 6 的页面(查看).如果你希望你的页面也更加有趣,那么你可以试试 WOW.js ...
- Music in Car CodeForces - 746F
Music in Car CodeForces - 746F 题意很难懂啊... 题意:http://blog.csdn.net/a838502647/article/details/74831793 ...