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

python题目:一个小小的猜名有戏_第1张图片python题目:一个小小的猜名有戏_第2张图片python题目:一个小小的猜名有戏_第3张图片python题目:一个小小的猜名有戏_第4张图片python题目:一个小小的猜名有戏_第5张图片

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

# 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



你可能感兴趣的:(python题目:一个小小的猜名有戏)