• 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('错误的指令,游戏结束')
  • 相关阅读:
    [AWS] Export and Import Data from DynamoDB Table 从 DynamoDB 数据库中导入和导出数据
    [LeetCode] 944. Delete Columns to Make Sorted 删除列使其有序
    [LeetCode] 943. Find the Shortest Superstring 找到最短的超级字符串
    [LeetCode] 942. DI String Match 增减DI字符串匹配
    [LeetCode] 941. Valid Mountain Array 验证山形数组
    [LeetCode] 940. Distinct Subsequences II 不同的子序列之二
    [LeetCode] 939. Minimum Area Rectangle 面积最小的矩形
    [LeetCode] 938. Range Sum of BST 二叉搜索树的区间和
    [LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序
    [LeetCode] 936. Stamping The Sequence 戳印序列
  • 原文地址:https://www.cnblogs.com/x945669/p/12450749.html
Copyright © 2020-2023  润新知