|
发表于 2018-4-9 20:55:19
|
显示全部楼层
def cut(number):
a = int(number)%10
b = int(((int(number)-a)%100)/10)
c = int(((int(number)-a-b*10)/1000)%10)
d = int(int(number)//1000)
list_input = [d,c,b,a]
li_0 = list_input[0]
li_1 = list_input[1]
li_2 = list_input[2]
li_3 = list_input[3]
list_output = [1,1,1,1]
if li_0 in list_guess and li_0 == list_guess[0]:
list_output[0] = 'A'
elif li_0 in list_guess:
list_output[0] = 'B'
if li_1 in list_guess and li_1 == list_guess[1]:
list_output[1] = 'A'
elif li_1 in list_guess:
list_output[1] = 'B'
if li_2 in list_guess and li_2 == list_guess[2]:
list_output[2] = 'A'
elif li_2 in list_guess:
list_output[2] = 'B'
if li_3 in list_guess and li_3 == list_guess[3]:
list_output[3] = 'A'
elif li_3 in list_guess:
list_output[3] = 'B'
a = list_output.count('A')
b = list_output.count('B')
print ('%dA%dB'%(a,b))
import random
list_1 = [1,2,3,4,5,6,7,8,9]
list_2 = [0,1,2,3,4,5,6,7,8,9]
number1 = random.choice(list_1)
list_2.remove(number1)
number2 = random.choice(list_2)
list_2.remove(number2)
number3 = random.choice(list_2)
list_2.remove(number3)
number4 = random.choice(list_2)
num=int(number1)*1000+int(number2)*100+int(number3)*10+int(number4)
list_guess=[number1,number2,number3,number4] #随机数生成
guess = 0
while True:
num_input = input('请输入一个四位数字:')
guess += 1
if int(num_input) == int('0905'):
break
else:
if not num_input.isdigit():
print('请输入数字!')
elif int(num_input)<1111 or int(num_input)>9999:
print('请输入四位数!')
else:
if int(num_input) == num:
print('恭喜你,猜对了!共猜了%d次!'%guess)
break
else:
cut(int(num_input)) |
|