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. 数据导入 ...
随机推荐
- flex兼容新
/* 子元素-平均分栏 */.flex1 { -webkit-box-flex: 1; /* OLD - iOS 6-, Safari 3.1-6 */ -moz-box-flex: 1; /* OL ...
- C# 利用Aspose.Words .dll将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)
下载Aspose.Words .dll http://pan.baidu.com/s/1c8659k 在vs2010中新建窗体应用程序,命名为 wordtopdf 添加Aspose.Words .d ...
- Android Service完全解析,关于服务你所需知道的一切(上) (转载)
转自:http://blog.csdn.net/guolin_blog/article/details/11952435 转载请注明出处:http://blog.csdn.net/guolin_blo ...
- PCB 线路铜皮面积(残铜率)计算的实现方法
一个多月没更新博客园了,这里继续分享关于PCB工程相关一些知识,做过PCB工程都知道用使用genesis或incam是可以非常方便的计算得到铜皮面积这个参数[下图],但实际这个软件是通过什么算法计算出 ...
- BestCoder Round #74 (div.1) 1002Shortest Path(hdoj5636)
哈哈哈哈,我就知道这道题目再扔给我,我还是不会,就是这么菜,哈哈哈 一开始官方题解就没搞懂-然后就看了一下别人的代码,水水过就算了.今天拿到-GG: 题意: 一开始,有一张原图,有一条长度为n的链. ...
- P5154 数列游戏(区间dp)
传送门 果然和dp有关的东西我绝对做不出来啊-- 设\(dp[i][j]\)表示消完区间\([i,j]\)中的数之后能得到的最大分数,如果消不完则为\(-inf\),否则枚举断点.顺便如果\(a[i] ...
- EasyUI创建选项卡并判断是否打开
//创建选项卡:判断选项卡是否打开,如果以打开则定位到选项卡,否则创建 function addPanel(title) { var bol = $('#main_tabs').tabs('exist ...
- git apply
1. git 安装 2.git 与服务器的验证 1.生成ssh ssh-keygen -t rsa -C "1107247128@qq.com" 2.查看生成的pub文件, ...
- 定位,标记,Socket通信传输位置
# -*- coding: utf- -*- """ Editor : X-POWER Date : -- Motto : talk is cheap,show me y ...
- iOS UITableView reloadData 刷新结束后执行后续操作
如果在reloadData后需要立即获取tableview的cell.高度,或者需要滚动tableview. 如果直接在reloadData后执行代码是有可能出问题的,比如indexPath为nil等 ...