zoukankan      html  css  js  c++  java
  • python基础

    python自学了一段时间感觉效果不佳。培养逻辑思维真的很重要。

    今天来做一个小游戏

    做一个猜年龄的小游戏;

    需求:

    给用户一个年龄大小,让用户来猜测多大。三次机会,如果三次都没猜中,用户可以选择不猜退出或继续猜测。

    需求分析:

    技术栈:if  else 条件判断肯定有 , for 循环好像不合适 ,while 循环可以 ,break (退出当前循环)

    要考虑程序的健壮性,要用异常处理 try.....except。

    思维导图:

    程序:

    from random import Random
    #猜年龄的游戏
    #定义一个年龄



    def age_choices(nums=2):
    age_lists = []
    age_all_choices_nums = '0123456789'
    age_length = len(age_all_choices_nums)-1
    random = Random()
    for i in range(nums):
    age = age_all_choices_nums[random.randint(0, age_length)] # 这里用到字符串的切片
    age_lists.append(age)
    age_str = ''.join(age_lists)


    return int(age_str)

    age_of_men = age_choices()

    print(age_of_men)
    i = 0
    while i <= 3:
    b = 1
    while b:
    try:
    a = int(input('请输入你猜测的年龄:'))
    except (IOError, ValueError):
    print('请重新输入你的年龄(必须是正整数)')
    else:
    b = 0 if isinstance(a, int) else 1

    if age_of_men == a:
    game_over = input('恭喜用户猜对了,请输入(y/n)按y退出游戏,按n游戏继续:')
    print('该用户年龄' + game_over + "岁")
    if game_over == 'y':
    print('-----------游戏结束--------------')
    break
    elif game_over == 'n':
    age_of_men = age_choices()
    print('--------游戏继续-----------------')

    elif age_of_men != a:
    i += 1
    print('不好意思你没有猜对哦!请继续加油')
    if i > 2:
    print('你已经猜错三次了')
    come_on = input('请输入(y/n)按y退出游戏有,按n游戏继续:')
    if come_on == 'n':
    age_of_men = age_choices()
    i = 0
    print('游戏继续')
    else:
    print('游戏结束')
    break
  • 相关阅读:
    Remote desktop manager共享账号
    content is not supported outside 'script" or asp content' region
    How to pass values across the pages in ASP.net without using Session
    GitLab Flow
    C#如何获取系统downloads和documents路径
    sql server查询结果复制出来,没有换行(存进去的数据是换行的)
    Type Interceptors
    JsonNode、JsonObject常用方法
    java获取当前时间戳的方法
    Java中float/double取值范围与精度
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/9086042.html
Copyright © 2011-2022 走看看