zoukankan      html  css  js  c++  java
  • 一段简单的猜数字代码

    一段简单的猜数字代码,要求是1,要猜的数字是随机数字1到9;2,猜数字次数为三次;3,如果猜中就打印提示语,并且结束程序;4,如果猜错就打印正确值还有剩下的次数;5,如果次数为0,就打印结束,欢迎下次再来。

    文件名为:easy_guess.py,代码如下:

     1 # !usr/bin/env python3
     2 # *-* coding:utf-8 *-*
     3 
     4 '''
     5 一个简单的猜数字游戏
     6 要猜的数字为随机数字1到9
     7 猜的次数为3
     8 如果猜中就打印猜中的提示语
     9 如果没猜中就打印正确值,还有剩下的次数
    10 如果次数为0,就打印结束,欢迎下次再来
    11 '''
    12 
    13 from random import randint #导入模块
    14     
    15 
    16 #num_input = int(input("Please input a number(range 1 to 9 ) to continue: ")) #输入数字
    17 guess_time = 0 #定义猜数字次数
    18 
    19 '''开始主循环'''
    20 while guess_time < 4: 
    21     num_input = int(input("PLease input a number(range 1 to 9) to continue: ")) #开始输入字符,因为从终端输入python认为是字符串,所以要在最前面用int()函数强制转化为整型,不然后续比较的时候会出现错误;
    22     number = randint(1,9) #定义随机数字,从1到9
    23     remain_time = 3 - guess_time #定义剩下的猜字次数
    24     
    25     if num_input == number: #比较输入的数字是否和随机数相等,代码的第21行前如果没有转化成整型,这里会提示str不能与int进行比较;
    26         print("Great guess, you are right!") #相等就执行
    27         break #跳出主循环,后续的代码都不会再执行
    28     elif num_input > number: #比较输入的数字是否大于随机数
    29         print("Large
     The right number is: {}
     You have {} chances!".format(number,remain_time)) #满足条件就提示正确答案和剩余次数
    30     elif num_input < number:
    31         print("Small
     The right number is: {}
     You have {} chances!".format(number,remain_time)) #满足条件就提示正确答案和剩余次数
    32 
    33     guess_time += 1 #每次循环完成都让猜字次数(guess_time)自加1,直到不满足主循环条件guess_time < 4

    上面的代码并不能执行如果次数为0 就打印结束,欢迎下次再来,而且也没有判断用户输入,下面把代码改一下,来完善一下,文件名为another_easy_guess.py:

     1 # usr/bin/env python3
     2 # *-* coding:utf-8 *-*
     3 
     4 from random import randint #导入模块
     5 
     6 guess_time = 0 #定义猜数字次数
     7 
     8 '''开始主循环'''
     9 while guess_time < 4: 
    10     remain_time = 3 - guess_time #定义猜的次数
    11     num_input = input("Please input a number(integer range 1 to 9) to continue(You have {} times to guess): ".format(remain_time)) #开始输入
    12     number = randint(1,9) #定义随机数
    13     
    14     
    15     if guess_time >=0 and guess_time < remain_time: #猜的次数大于0还有小于剩余次数才会执行下面的代码块
    16         if not num_input.isdigit(): #判定输入的是否是数字
    17             print("Please input a integer to continue.") #如果不是数字,提示用户输入数字
    18         elif int(num_input) < 0 or int(num_input) > 10: #判定是不是在我们设定的数字范围内
    19             print("Please use the number 1 to 9 to compare.") #如果不是就提示
    20         elif int(num_input) == number: #判定输入的数字是否与随机数相等
    21             print("Great guess, you are right!")
    22             break
    23         elif int(num_input) > number: #判定输入数是否大于随机数
    24             print("Large
     The right number is: {}
     There are {} chances for you!".format(number,(remain_time - 1)))
    25         elif int(num_input) < number: #判定输入数是否小于随机数
    26             print("Small
     The right number is: {}
     There are {} chances for you!".format(number,(remain_time - 1)))
    27     else:
    28         print("You have arrived the limited, see you next time!") #次数小于剩余次数后执行
    29         break #跳出循环
    30         
    31     guess_time += 1 #猜的次数自增1直到guess_time < 4;
    32     
    33 
    34 '''历史遗留问题:1,上面的代码只针对用户输入的数字,用户输入字符串也是会计算次数的;
    35                 2,如果都没猜中且次数用完,是直接打印最后的You have arrived the limited, see you next time!而预期的提示正确答案。
    36 '''

    上面代码只针对用户输入的是数字,如果输入的是字符串,比如aaa,bbb也是会计算次数的。

  • 相关阅读:
    MyBatis Generator去掉生成的注解
    IDEA git修改远程仓库地址
    Spring Boot 集成druid
    解决 SpringBoot 没有主清单属性
    Intellij IDEA 安装lombok及使用详解
    SET FOREIGN_KEY_CHECKS=0;在Mysql中取消外键约束
    @SpringBootApplication
    IDEA 创建git 分支 拉取分支
    Intellij Idea 将java项目打包成jar
    Spring Could Stream 基本用法
  • 原文地址:https://www.cnblogs.com/mafu/p/13129428.html
Copyright © 2011-2022 走看看