zoukankan      html  css  js  c++  java
  • while练习题

    # 1. 使用while循环输出1 2 3 4 5 6     8 9 10
    count = 1
    while count <= 10:
    if count == 7:
    count += 1
    continue
    print(count)
    count += 1
    # 2. 求1-100的所有数的和
    res=0
    count = 1
    while count<=100:
    res += count #res=res+count
    count+=1 #count=count+1
    print(res)
     # 3. 输出 1-100 内的所有奇数
    count = 1
    while count <= 100:
    if count % 2 != 0:
    print(count)
    count += 1
    # 4. 输出 1-100 内的所有偶数
    count = 1
    while count <= 100:
    if count % 2 == 0:
    print(count)
    count += 1

    # 5. 求1-2+3-4+5 ... 99的所有数的和
    res = 0
    count = 1
    while count <100:
    if count % 2 == 0:
    res -= count
    else:
    res += count
      count+=1
    print(res)
    # 6. 用户登陆(三次机会重试)
    print('start')
    count=0
    while count<=2:
    name=input('your name: ')
    pwd=input('your password;')
    if name=='summer'and pwd=='12345':
    print('success')
    break
    else:
    print('user or password err')
    count+=1
    else:
    print('end')
    #7:猜年龄游戏
    要求:
        允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
    age = 18
    count = 0
    while count <= 2:
    guess = int(input('age:'))
    if guess == age:
    print('恭喜')
    break
    else:
    print('err')
    count += 1
    else:
    print('退出')
    #8:猜年龄游戏升级版 
    要求:
        允许用户最多尝试3次
        每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
        如何猜对了,就直接退出 
    age = 18
    count = 0
    while True:
    if count == 3:
    choice = input('继续Y/N:')
    if choice == 'Y' or choice == 'y':
    count = 0
    else:
    break
    guess = int(input('age:'))
    if guess == age:
    print('success')
    break
    count += 1
     
     
  • 相关阅读:
    【stanford】梯度、梯度下降,随机梯度下降
    [philosophy]空间
    【crawler】heritrix 3 使用
    【database】database domain knowledge
    【java】Java异常处理总结
    【computer theory】一、集合、关系和语言
    【java】ubuntu部署web项目war包到tomcat上
    【MachineLeaning】stanford lesson one
    【数据立方】由表和电子数据表到数据立方体,cuboid方体
    PHP变参函数的实现
  • 原文地址:https://www.cnblogs.com/xiamenghan/p/9656346.html
Copyright © 2011-2022 走看看