#一些东西采用了偷懒的处理方法。。。感觉写Python比较舒服的地方是括号
#少,不习惯的地方是错误处理方法与perl,c,shell都不一样。其它,刚开始
#学,还没太多感慨,Python确实值得学,可是用师兄的话来说,我俩在进行
#“速成”式的学习,写一个小程序,帮助记忆一下基本语法吧。
#!/usr/bin/python
import random
orig_num=[11,11,11,11]
def generate_rand():
i=0
while i<4:
rand=random.randrange(0,10)
if i==0 and rand==0:
continue
else:
for j in range(0,i):
if rand==orig_num[j]:
rand=11
break
if rand<10:
orig_num[i]=rand
i=i+1
# print orig_num
generate_rand()
i=1
while i<9:
a=0
b=0
guess_input=raw_input('Please input the number you guessed: ')
if len(guess_input)!=4:
print 'You should input a integer less than 10000 and greater than 1000'
continue
try:
guess=int(guess_input)
except ValueError:
print 'You should enter an integer less than 10000 and greater than 1000!'
continue
check_guess=[guess/1000,guess%1000/100,guess%100/10,guess%10]
for j in range(0,4):
for k in range(0,4):
if orig_num[j]==check_guess[k]:
if j==k:
a=a+1
else:
b=b+1
i=i+1
if a==4:
print 'You win! ^&^'
break
else:
print '%d A %d B'%(a,b)
if i>8:
print 'You lose. The number is %d%d%d%d' %(orig_num[0],orig_num[1],orig_num[2],orig_num[3])