加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 猜数字游戏

发布时间:2020-12-17 17:01:39 所属栏目:Python 来源:网络整理
导读:# -*- coding:utf-8 -*- import random def cmp(x,y): ? ? if x y : ? ? ? ? print "数字小了" ? ? ? ? return False ? ? elif x y: ? ? ? ? print "数字大了" ? ? ? ? return False ? ? else: ? ? ? ? print "恭喜你答对了" ? ? ? ? return True file = ope

# -*- coding:utf-8 -*-

import random

def cmp(x,y):

? ? if x > y :

? ? ? ? print "数字小了"

? ? ? ? return False

? ? elif x < y:

? ? ? ? print "数字大了"

? ? ? ? return False

? ? else:

? ? ? ? print "恭喜你答对了"

? ? ? ? return True

file = open(r'D:pythonCrossinrecord.txt','r')

score = file.read().split()

file.close()

#一共玩游戏的次数,最快猜对的轮数,总共玩的次数

game_times,min_times,total_times= int(score[0]),int(score[1]),int(score[2])

print game_times,total_times

# 平均每次猜对用的轮数

if game_times > 0:

? ? avg_times = total_times / game_times

else:

? ? avg_times = 0

print "你一共玩游戏的次数:%d,最快猜出的轮数是:%d,,平均每次猜对用的轮数:%d"

? ? ? % (game_times,avg_times)

print "猜数字游戏即将开始"

total = int(random.randint(0,9))

print total

times = 0

result = False

while result == False:

? ? number = int(raw_input("请输入一个0-9的整数:"))

? ? result = cmp(total,number)

? ? times += 1

? ? print "请重新输入一个数字"

#游戏次数

game_times += 1

#判断是否最快猜对的轮数

if game_times != 0 or times < min_times:

? ? min_times = times

#一共玩的轮数

total_times += times

print game_times,total_times

result = '%dt %dt %d' % (game_times,total_times)

file = open(r'D:pythonCrossinrecord.txt','w')

file.write(result)

file.close()


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读