• Python小游戏 -- 猜数字


    Python初学者小游戏:猜数字


    游戏逻辑:电脑随机生成一个数字,然后玩家猜数字,电脑提示猜的数字大了还是小了,供玩家缩小数字范围,达到既定次数后,玩家失败。若在次数内猜对,玩家获胜。


    涉及知识点:random.randint() , print() , input() ( raw_input() )


    参考实现代码


    #!/usr/bin/env python
    # encoding: utf-8
    
    #使用print("",end=...)标准
    from __future__ import print_function
    
    import os
    import sys
    import time
    import random
    
    #输入检测
    
    while 1:
    	os.system('cls')
    	print ("Hello , Welcome to Guess_Number Games...The Number is between 1 - 10...")
    	print ("Please input the level you want(1~10): ",end = '')
    	level = raw_input("")
    	diff = 11-int(level)
    	if diff > 10 or diff <1:
    		print ("Invalid Input...")
    		time.sleep(0.3)
    	else:
    		break
    
    #猜数字流程
    
    count_num = 0
    ran = random.randint(1,10)
    while count_num < diff:
    	count_num += 1
    	print (str(count_num)+": "+"Please input the number you guess: ",end = '')
    	number = raw_input()
    	number = int(number)
    	if number < ran:
    		print ("Too Little...")
    		continue
    	elif number > ran:
    		print ("Too Big...")
    		continue
    	else:
    		print ("Congraduation! You Win...")
    		break
    if count_num == diff:
    	print ("You Lose...")


  • 相关阅读:
    07组 Beta冲刺 (2/5)
    第07组 Beta冲刺 (1/5)
    第07组 Alpha冲刺 总结
    ES相关
    集群与分布式
    idea使用总结
    Tomcat配置与启动与访问
    Web基础
    B/S与C/S架构
    一周视频学习总结
  • 原文地址:https://www.cnblogs.com/csnd/p/12897063.html
Copyright © 2020-2023  润新知