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
     
     
  • 相关阅读:
    使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件
    Mybatis学习 PageHelper分页插件
    mysql 5.1.7.17 zip安装 和 隔段时间服务不见了处理
    使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
    一位资深程序员大牛给予Java初学者的学习建议
    数据结构和算法学习 -- 线性表
    多线程的实现方式区别
    Log4j.properties属性文件
    Java自定义注解
    Spring配置属性文件
  • 原文地址:https://www.cnblogs.com/xiamenghan/p/9656346.html
Copyright © 2011-2022 走看看