前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >while循环,逻辑语句.

while循环,逻辑语句.

作者头像
py3study
发布2020-01-16 15:39:51
8240
发布2020-01-16 15:39:51
举报
文章被收录于专栏:python3python3
1.猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;
只有等于66,显示猜测结果正确,然后退出循环。
while 1:
    num = int(input('请输入一个数字:'))
    if num > 66:
        print('猜测的结果大了')
    elif num < 66:
        print('猜测的结果小了')
    else:
        print('猜测结果正确')
        break

2.在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,
则自动退出循环,并显示‘大笨蛋’。
count = 3
while count > 0:
    num = int(input('请输入一个数字:'))
    if num > 66:
        print('猜测的结果大了')
    elif num < 66:
        print('猜测的结果小了')
    else:
        print('猜测结果正确')
        break
    count -= 1
if count == 0:
    print('大笨蛋!')

3.使用两种方法实现输出 1 2 3 4 5 6   8 9 10 。
(1)count = 1
while count < 11:
    if count != 7:
        print(count)
    count += 1
(2)count = 1
while count < 11:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1

4.求1-100的所有数的和
count = 1
sum = 0
while count < 101:
    sum += count
    count += 1
print(sum)

5.输出 1-100 内的所有奇数
count = 1
while count <= 100:
    if count % 2 ==1:
        print(count)
    count += 1

6.输出 1-100 内的所有偶数
count = 1
while count <= 100:
    if count % 2 ==0:
        print(count)
    count += 1

7.求1-2+3-4+5 ... 99的所有数的和
count = 1
sum = 0
while count <= 99:
    if count % 2 ==1:
        sum += count
    elif count % 2 ==0:
        sum -= count
    count += 1
print(sum)

8.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
count = 3
while count > 0:
    username = input('请输入用户名:')
    pwd = input('请输入密码:')
    if username == 'qwe' and pwd == '123':
        print('登陆成功!')
        break
    count -= 1
    print('输入错误,剩余%s次机会' % count)
else:
  print("三次机会用完!")
9.简述ASCII、Unicode、utf-8编码
ASCII码:英文和标点字符,8位,1个字符.2**8
Unicode:万国码,32位,4个字符.世界通用.2**32
UTF-8:简化万国码,英文8位1字符,欧洲16位2字符,中文24位3字符.

10.简述位和字节的关系?
8位 为 1个字节
8bit = 1byte
1位代表一个0/1

11.猜年龄游戏
要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
count = 3
while count > 0:
    age = int(input('请猜:'))
    if age == 24:
        print('恭喜你猜对啦')
        break
    else:
        count -= 1
        print('猜错了,还剩%s次机会!' % count)

12.猜年龄游戏升级版
要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如
果回答N,就退出程序,如何猜对了,就直接退出。
while 1:
    count = 3
    while count > 0:
        age = int(input('请猜:'))
        if age == 24:
            print('恭喜你猜对啦')
            break
        else:
            count -= 1
            print('猜错了,还剩%s次机会!' % count)
    yn = input('是否还想继续玩,回复Y继续,回复N退出:')
    if yn.upper() == 'Y':
        continue
    elif yn.upper() == 'N':
        break

13.判断下列逻辑语句的True,False

- 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
True
not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
False

14.求出下列逻辑语句的值。

8 or 3 and 4 or 2 and 0 or 9 and 7
8
0 or 2 and 3 and 4 or 6 and 0 or 3
4

15.下列结果是什么?

6 or 2 > 1
6
3 or 2 > 1
3
0 or 5 < 4
f
5 < 4 or 3
3
2 > 1 or 6
t
3 and 2 > 1
t
0 and 3 > 1
0
2 > 1 and 3
3
3 > 1 and 0
0
3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
2
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档