python 版猜数字游戏
2014-05-03 14:52
471 查看
初学 python,写了一个小小的toy code 来练习,bug 肯定有,欢迎提出意见
#! /usr/bin/python
#This is guess number Game written in python
import random
running = True
maxTrial = 10
guessTimes = 0
answerlist = ['0', '0', '0', '0']
guessList = ['0', '0', '0', '0']
def hasCommonNumber(list):
for i in range(0, 4):
for j in range(i+1, 4):
if list[i] == list[j]:
return True
return False
# Check the guessNumber and give a hit for it
# A:right number in the right position
# B:right number in the wrong position
def isRightAnswer(guessNumber):
global running
global guessTimes
guessTimes += 1
flagA = 0
flagB = 0
guessList[0] = guessNumber / 1000
guessList[1] = (guessNumber % 1000) / 100
guessList[2] = ((guessNumber % 1000) % 100) / 10
guessList[3] = guessNumber % 10
#check the guess list
print(guessList)
for i in range(0, 4):
for j in range(0, 4):
if answerlist[i] == guessList[j]:
if i == j:
flagA += 1
else:
flagB += 1
if flagA == 4:
print 'You Got It, The Answer Is', answerlist
running = False
elif guessTimes >= maxTrial:
print 'Sorry,After ', guessTimes, 'times guessing you just can\'t get it'
running = False
else:
print flagA, 'A', flagB, 'B'
# generate the answer, and make sure there's no common numbers
while True:
for i in range(0, 4):
answerlist[i] = random.randrange(10)
if hasCommonNumber(answerlist) == False:
break
while running:
# check the seq
print(answerlist)
guessNumber = (int(raw_input("Make A Guess: ")))
isRightAnswer(guessNumber)
else:
print 'Game Exit!'
#! /usr/bin/python
#This is guess number Game written in python
import random
running = True
maxTrial = 10
guessTimes = 0
answerlist = ['0', '0', '0', '0']
guessList = ['0', '0', '0', '0']
def hasCommonNumber(list):
for i in range(0, 4):
for j in range(i+1, 4):
if list[i] == list[j]:
return True
return False
# Check the guessNumber and give a hit for it
# A:right number in the right position
# B:right number in the wrong position
def isRightAnswer(guessNumber):
global running
global guessTimes
guessTimes += 1
flagA = 0
flagB = 0
guessList[0] = guessNumber / 1000
guessList[1] = (guessNumber % 1000) / 100
guessList[2] = ((guessNumber % 1000) % 100) / 10
guessList[3] = guessNumber % 10
#check the guess list
print(guessList)
for i in range(0, 4):
for j in range(0, 4):
if answerlist[i] == guessList[j]:
if i == j:
flagA += 1
else:
flagB += 1
if flagA == 4:
print 'You Got It, The Answer Is', answerlist
running = False
elif guessTimes >= maxTrial:
print 'Sorry,After ', guessTimes, 'times guessing you just can\'t get it'
running = False
else:
print flagA, 'A', flagB, 'B'
# generate the answer, and make sure there's no common numbers
while True:
for i in range(0, 4):
answerlist[i] = random.randrange(10)
if hasCommonNumber(answerlist) == False:
break
while running:
# check the seq
print(answerlist)
guessNumber = (int(raw_input("Make A Guess: ")))
isRightAnswer(guessNumber)
else:
print 'Game Exit!'
相关文章推荐
- Python: 渐进猜数字游戏 <6> 运算符和函数
- Python: 渐进猜数字游戏 <4> 数据类型及随机数
- Python: 渐进猜数字游戏 <3> 基本输入输出
- python实现猜数字游戏(无重复数字)示例分享
- 猜数字游戏-python
- python之猜数字游戏
- Python: 渐进猜数字游戏 <5> 控制语句
- 【python小练习】简单的猜数字游戏
- 第22节--python猜数字游戏(图形界面)
- python入门——猜数字游戏
- Python入门——猜数字游戏
- Python3.6:“猜数字游戏”TypeError: '<' not supported between instances of 'str' and 'int'
- Python: 渐进猜数字游戏 <8> 代码优化 与 结项
- Python: 渐进猜数字游戏 <1> 代码、测试与猜数字
- python学习笔记 ——python写的猜数字游戏 002
- 猜数字游戏--基于python
- Python猜数字游戏
- Python: 渐进猜数字游戏 <7> 数字比较 与 猜数字游戏V0.1版
- python-猜数字游戏
- Python 小案例 猜数字游戏