pa = ['1','2','3','4','5','6','7','8','9','0',]
password = sample(pa,4)
#print password
mypass = '4A0B'
result = ['']*4
while ''.join(result) != mypass:
result = ['']*4
i = 0
j = 0
text = raw_input('Please guess the password of 4 unrepeated numbers 0-9:')
for x,y in zip(text,password):
if x == y:
i += 1
for z in text:
if z in password:
j += 1
作者: 渡俊 时间: 2016-4-15 22:41
import random
l = [0,1,2,3,4,5,6,7,8,9]
du = ""
for i in range(4):
ff = random.choice(l)
du += str(ff)
l.remove(ff)
print("答案是"+ du )
def guess(du):
you = input("请输入四位数 不能重复")
if you == du:
print("good")
else :
right_nume = 0
wrong_nume = 0
i = 0
while i < 4:
if you[i] in du:
if you[i] == du[i]:
right_nume += 1
else :
wrong_nume += 1
i += 1
print(str(right_nume) + "A" + str(wrong_nume) + "B")
guess(du)
guess(du) 作者: morpheus2222 时间: 2017-1-23 10:25
import random
s = range(10)
password = random.sample(s,4)
password = list(map(str,password))
#print(password)
dic_password = dict(list(enumerate(password)))
#print(dic_password)
win = False
while win ==False:
guess = input("guess the password(4 digtals):")
dic_guess = dict(list(enumerate(list(guess))))
#print(dic_guess)
A = 0
B = 0
for i in dic_password:
for j in dic_guess:
if i == j:
if dic_password[i] == dic_guess[j]:
A += 1
else:
if dic_password[i] == dic_guess[j]:
B += 1
result = str(A)+"A"+str(B)+"B"
print(result)
if result == "4A0B":
win = True
print("you win!")
挺好的
随机数那里,看看random模块,有更好的方法作者: 木鱼 时间: 2017-8-9 21:22
import random ,string
class game():
a=0
def start(self):
passwd = random.sample(string.digits, 4)
while True:
anser=input('输入四位密码:')
if len(anser)==4:
' '.join(anser).split()
self.test(anser,passwd)
if game.a==4:
break
print('密码错误')
print('密码正确!')
next=input('输入‘1’进入下一轮,任意键结束。')
if next==1:
game.a=0
self.start()
exit(0)
def test(self,ans,pd):
j=a=b=0
for i in range(4):
if ans[i]==pd[i]:
a+=1
continue
if ans[i] in pd:
b+=1
print('%dA%dB' % (a, b))
if a==4:game.a=4
if __name__=='__main__':
s=game()
s.start()作者: wolfog 时间: 2017-8-17 17:04
import random
def createPwd():
numList = range(0, 10)
numListStr = [str(i) for i in numList]
return "".join(random.sample(numListStr, 4))
pwd = createPwd()
param = False
while not param:
numA = 0
numB = 0
inputStr = raw_input("请输入四个数字:")
if len(inputStr) != 4:
print "请输入4位密码"
break
for i in range(0, 4):
if inputStr[i] == pwd[i]:
numA += 1
elif pwd.find(inputStr[i]) != -1:
numB += 1
if i == 3:
if numA == 4:
param = True
print "4A0B"
else:
print str(numA) + "A" + str(numB) + "B" 作者: FERRYMAN 时间: 2021-2-12 18:35
写得挺顺的,30分钟把主体写完了。但是修改和完善内容用了一个半小时。