zoukankan      html  css  js  c++  java
  • Python学习————while循环作业

    # 作业(必做题):
    # 1. 使用while循环输出1 2 3 4 5 6 8 9 10
    count = 1
    while count < 7:
    print(count)
    count = count + 1
    count = count + 1
    while count < 11:
    print(count)
    count = count + 1
    # 2. 求1-100的所有数的和
    x = 0
    for y in range(1, 101):
    x = x + y
    # print(x)
    # 3. 输出 1-100 内的所有奇数
    count = 1
    while count < 101:
    if count % 2 == 0:
    count = count + 1
    else:
    print(count, '奇数')
    count = count + 1
    # 4. 输出 1-100 内的所有偶数
    count = 1
    while count < 101:
    if count % 2 == 1:
    count = count + 1
    else:
    print(count, '偶数')
    count = count + 1
    # 5. 求1-2+3-4+5 ... 99的所有数的和
    count1 = 1
    count2 = 0
    while count1 < 100:
    temp = count1 % 2
    if temp == 0:
    count2 = count2 - count1
    else:
    count2 = count2 + count1
    count1 = count1 + 1
    print(count2)

    # 6. 用户登陆(三次机会重试)
    username = 'yan'
    password = 123
    count = 0
    while count < 3:
    name = input('请输入用户名 :')
    password = input('请输入密码 :')
    if name == username and password == int(password):
    print('登录成功')
    break
    else:
    print('登录失败')
    count += 1
    # 7:猜年龄游戏
    # 要求:
    # 允许用户最多尝试3次,3
    # 次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
    age = 80
    count = 0
    print('猜一猜 这个数字是多少(范围在 1-100)')
    while count < 3:
    guess = int(input('请输入你猜的数字 :'))
    if guess < age:
    print('猜小了')
    elif guess > age:
    print('猜大了')
    else:
    print('恭喜你猜对了')
    break
    count += 1
    else:
    print('很遗憾机会用光了,你并没有猜对')
    # # 8:猜年龄游戏升级版(选做题)
    # 要求:
    # 允许用户最多尝试3次
    # 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
    # 如何猜对了,就直接退出
    age = 80
    count = 0
    print('猜一猜 这个数字是多少(范围在 1-100)')
    while count < 3:
    guess = int(input('请输入你猜的数字 :'))
    if guess == age:
    print('恭喜你猜对了')
    break
    elif guess < age:
    print('猜小了')
    count += 1
    else:
    print('猜大了')
    count += 1
    if count == 3:
    choice = input('你已经用光三次机会请问是否继续, Y/N :')
    if choice == 'Y' or choice == 'y':
    print('你选择了: ', choice, '游戏将继续进行, 欢迎游玩')
    count = 0
    elif choice == 'N' or choice == 'n':
    print('你选择了: ', choice, '程序即将退出, 欢迎游玩')
    break
    else:
    print('错误的指令,游戏结束')
  • 相关阅读:
    HTML5之Canvas绘图(二) ——应用篇之七巧板
    Mock分页
    ant design pro 实战 : 使用 ztree
    echarts 实战 : 怎么写出和自动生成的一样的 tooltip ?
    react实战 : react 与 canvas
    react实战 : react 与 svg
    echarts 踩坑 : 为什么效果出不来?看看有没有正确引入
    react实战 : 用矩阵思想做一个自适应布局容器组件
    echarts 踩坑 : 为什么触摸柱状图的时后柱子不见了?原来是color的锅!
    echarts 实战 : 图表竖着或横着是怎样判定的?
  • 原文地址:https://www.cnblogs.com/x945669/p/12450749.html
Copyright © 2011-2022 走看看