python题目:一个小小的猜名有戏


代码逻辑(无图形界面和输入):

# coding:utf-8
from random import randint
global errCount
def pickName():
	nameList = ["ALAN TURING", "DONALD KNUTH", "ADA LOVELACE", "GRACE HOPPER", "GORDON MOORE"]
	return nameList[randint(0, 4)]

def drawMan(errCount):
	if errCount == 1: print "head"
	if errCount == 2: print "body"
	if errCount == 3: print "left hand"
	if errCount == 4: print "right hand"
	if errCount == 5: print "left foot"
	if errCount == 6: print "right foot"

if __name__ == "__main__":
	# name = pickName()
	name = "ALAN TURING"
	lenName = len(name)
	print "pick name:", name
	guessList = [""]*lenName
	print "guessList", guessList
	############### 循环输入输入 #############
	input = "AGIACLDNRUT" 	
	flag = 0
	errCount = 0 
	for inlet in input:
		flag = 1
		for i in range(lenName):		# 开始猜测
			if name[i] in name[:i-1]:	# 检测过的字母跳过
				continue
			if name[i] ==" ":			# 处理空格
				guessList[i] == " "
				blank = i
			if name[i] == inlet:
				guessList[i] == inlet
				flag = 0
		if flag == 1:					# 没有检测到
			errCount += 1
			drawMan(errCount)
	if errCount < 6: 
		print "Congratulations!"
	else: 
		print "You lost! The answer was:\n",name



发布了392 篇原创文章 · 获赞 487 · 访问量 240万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览