鱼C论坛

 找回密码
 立即注册
楼主: 冬雪雪冬

[技术交流] Python:每日一题 175

[复制链接]
发表于 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))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 21:28:01 From FishC Mobile | 显示全部楼层
塔利班 发表于 2018-4-8 12:59

你输入55544321试试看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 21:30:10 | 显示全部楼层
塔利班 发表于 2018-4-9 20:25
这个输入重复没提示也没序号

悄摸的修改了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 21:40:37 | 显示全部楼层
达锅 发表于 2018-4-9 21:28
你输入55544321试试看

。。肯定跳过啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 21:43:20 | 显示全部楼层
  1. import random
  2. #生成数字结果集
  3. randnum = []
  4. #生成数字总个数
  5. count = 0
  6. #生成一个随机数字
  7. temp = random.randint(0,9)
  8. #生成四位不重复的随机数
  9. history = []
  10. A = 0
  11. B = 0
  12. a = True
  13. while  a:
  14.     if temp not in randnum:
  15.         randnum.append(temp)
  16.         count += 1
  17.         if count == 4:
  18.             a = False
  19.     else:
  20.         temp = random.randint(0,9)
  21.         
  22. while True:      
  23.     my_num = input("请输入四个不重复的数字,Q-退出游戏:")
  24.     if my_num == 'Q':
  25.         break
  26.     else:
  27.         history.append(my_num)
  28.         li = []
  29.         li.extend(my_num)
  30.         print('HISTORY:')
  31.         for a in range(len(history)):
  32.             print(a+1)
  33.             for i in range(len(li)):  
  34.                 print(li[i],end='')
  35.             print(' --> ',end='')
  36. #判断对的个数
  37.             for i in str(randnum):
  38.                 for j in li:
  39.                     if i==j:
  40.                         B += 1
  41.             
  42.             for i in [0,1,2,3]:
  43.                 if  my_num[i] == str(randnum[i]):
  44.                     A +=1
  45.             print(str(A)+'A'+str(B)+'B')
  46. ##            将A,B数值归零
  47.             A = 0
  48.             B = 0
  49.         
  50. #初学一天python的小白,望各位大神多多指教
  51.             
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 21:48:00 From FishC Mobile | 显示全部楼层
塔利班 发表于 2018-4-9 21:40
。。肯定跳过啊

哪句代码限制了?我手机复制运行不了,回家看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 21:49:53 | 显示全部楼层
达锅 发表于 2018-4-9 21:48
哪句代码限制了?我手机复制运行不了,回家看看

都在手机上玩爬虫了,,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 22:07:16 From FishC Mobile | 显示全部楼层
塔利班 发表于 2018-4-8 12:59

我看来看去,你没有对a的长度进行判断啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 22:09:18 | 显示全部楼层
本帖最后由 塔利班 于 2018-4-9 22:11 编辑

哦,我明白了,你说的是正好输入了长度不为4却数字有4个的情况
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 23:12:33 | 显示全部楼层
Luke李 发表于 2018-4-9 10:25
import random

def random_number():

之前看题目描述里没提到输入重复的问题修改了一下,应该没问题了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-9 23:33:55 | 显示全部楼层
import random as r


def guess_game():
    num_list = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    num_1 = num_list[r.randint(1, len(num_list)-1)]
    num_list.remove(num_1)
    num_2 = num_list[r.randint(0, len(num_list)-1)]
    num_list.remove(num_2)
    num_3 = num_list[r.randint(0, len(num_list)-1)]
    num_list.remove(num_3)
    num_4 = num_list[r.randint(0, len(num_list)-1)]
    answer = num_1 + num_2 + num_3 + num_4
    guess = input('请输入不重复的四位数字:')
    history = []
    while True:
        if answer == guess:
            print('答对啦,恭喜恭喜!')
            break
        elif guess == 'Q':
            print('游戏退出')
            break
        else:
            count_a = 0
            count_b = 0
            for i in range(4):
                if guess[i] == answer[i]:
                    count_a += 1
                if (guess[i] in answer) and guess[i] != answer[i]:
                    count_b += 1
            history.append('. %s --> %dA%dB' % (guess, count_a, count_b))
            print('HISTORY:')
            for i in range(len(history)):
                print(i+1, history[i])
            guess = input('请输入不重复的四位数字:')
            continue


guess_game()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 00:32:16 | 显示全部楼层
本帖最后由 坑得飞起 于 2018-4-10 00:33 编辑

import random as rt
answer,i='',0
while i<4 :
    j=str(rt.randint(0,9))
    if j not in answer :
        answer,i=answer+j,i+1

        
jilu,his=0,'HISTORY:'
while True:
    temp=input('请输入不重複的四个数字, Q - 退出遊戲:')
    if temp=='Q':
        break
    Q=0
    for i in range(4):
        if temp.count(temp)>1 :
            Q=1
    if Q==1:
        Q=0
        continue
    if temp==answer :
        print('4A0B  猜对了')
        break
    a,b,jilu=0,0,jilu+1
    for i in range(4):
        if answer==temp:
            a+=1
        elif (temp in answer) :
            b+=1
    his+='\n'+str(jilu)+'.'+temp+'-->'+str(a)+'A'+str(b)+'B'
    print(his)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 10:20:08 | 显示全部楼层
本帖最后由 lhj390 于 2018-4-10 12:31 编辑
  1. import random
  2. def randomNum():
  3.     choicenums = [];
  4.     isChoice = 1;
  5.     choiceTimes = 0;
  6.     while isChoice:
  7.         num =random.choice(range(10));
  8.         if (choicenums.count(num)==0):
  9.             choicenums.append(num);
  10.             choiceTimes += 1;
  11.         if(choiceTimes==4):
  12.             isChoice = 0;
  13.             print(choicenums)
  14.     return (choicenums);

  15. def judgeNum():
  16.     result = ['',0];
  17.     result[0] = str(times)+'.'+inputNum+'-->';
  18.     for i in range(4):
  19.         if(int(inputNum[i]) in nums):
  20.             if(int(inputNum[i]) == nums[i]):
  21.                 result[0] += 'A';
  22.                 result[1] += 1;
  23.             else:
  24.                 result[0] += 'B';
  25.         else:
  26.             result[0] += inputNum[i];
  27.     return (result);

  28. times = 0;
  29. condition = 1;
  30. history = ['history:'];
  31. nums = randomNum();
  32. while condition:
  33.     inputNum = input('请输入不重复的四位数字(输入q退出游戏):');
  34.     isRepeat = 1;
  35.     if (inputNum=='q'):
  36.         print('退出游戏');
  37.         break;
  38.     for i in inputNum:
  39.         if (inputNum.count(i)>=2):
  40.             print('有重复数字,请重新输入');
  41.             isRepeat = 0;
  42.             break;
  43.     if (isRepeat):
  44.         times += 1;
  45.         judgeResult = judgeNum();
  46.         if judgeResult[1] == 4:
  47.             condition = 0;
  48.         history.append(judgeResult[0]);
  49.         for j in history:
  50.             print(j);
  51. else:
  52.     print('猜对了!');
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 10:26:33 | 显示全部楼层
@冬雪雪冬 我当时看题的时候没说要重复啊,只是说输一个数字显示几A几B
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 11:00:15 | 显示全部楼层
@冬雪雪东 这是按你改后题目的代码:
import random
a=[0,1,2,3,4,5,6,7,8,9]
b=random.sample(a,4)
while True:
    c=[int(i) for i in input('请输入四个不重复的数字,数字之间用逗号隔开').split(',')]
    while b[0]==0:
        print('系统所选四位数千位为零,系统重新选取四位数')
        b=random.sample(a,4)

    d=[d for d in c if d in b]
    B=len(d)
    A=0

    for j in range(4):
        if b[j]==c[j]:
            A+=1
            B-=1
   
    print('%dA%dB'%(A,B))
    if b==c or 'q'==input('你输入的数字不正确,你可以按任意键继续输入,也可以按q结束程序'):
        break
   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 11:08:40 | 显示全部楼层
fan1993423 发表于 2018-4-10 11:00
@冬雪雪东 这是按你改后题目的代码:
import random
a=[0,1,2,3,4,5,6,7,8,9]

:)出题本意就是4个数字,不是四位数,0123也可以。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 11:25:17 | 显示全部楼层
  1. # encoding: utf - 8
  2. # 计算机出一个四位数,各位数字不要重複,你猜这个四位数。
  3. # 猜测的结果将会列在自己的猜测历史列表,并以A和B来表示结果。
  4. # A代表猜测的数字中,数字相同且位置也正确的个数。
  5. # B代表猜测的数字中,数字相同但位置不一样的个数。
  6. # 举例来说,如果计算机出的的数字为1234,且你猜的数字为5283。
  7. # 其中2被猜到且位置正确,3也被猜到但位置不对,所以结果会出现1A1B。
  8. # 直至你猜对或输入q退出。
  9. import random


  10. # 用来判断猜中的个数与位置的个数
  11. def p(str1, str2):
  12.     a = 0
  13.     b = 0
  14.     for i in range(4):
  15.         if str2[i] == str1[i]:
  16.             a += 1
  17.         elif str2[i] in str1:
  18.             b += 1
  19.     return [a, b]


  20. # 产生四位数
  21. def creat_number():
  22.     while True:
  23.         a = str(random.randint(1234, 9876))
  24.         s = set(a)
  25.         if len(s) == 4:
  26.             break
  27.     return a


  28. def main():
  29.     print('--------guess number game--------')
  30.     result = creat_number()
  31.     # 方便测试,给出答案
  32.     print(result)
  33.     his = []
  34.     playgame = True
  35.     while playgame:
  36.         # 检验输入是否符合
  37.         goodnum = False
  38.         while not goodnum:
  39.             guess_num = input('请输入你要猜的数:')
  40.             if len(set(guess_num)) == 4:
  41.                 goodnum = True
  42.             else:
  43.                 print("你输入的数字有重复,请重新输入。")

  44.         a = p(result, guess_num)
  45.         if a[0] == 4:
  46.             print('你猜对了,好厉害啊!')
  47.             print('请按Q退出游戏,其他再来一次')
  48.             if input() == 'q' or input == 'Q':
  49.                 playgame = False
  50.             else:
  51.                 result = creat_number
  52.                 his.clear()
  53.         else:
  54.             his.append([guess_num, a])
  55.             for i in range(len(his)):
  56.                 if i == 0:
  57.                     print("历史记录")
  58.                 print(his[i][0], "%dA%dB" % (his[i][1][0], his[i][1][1]))
  59. if __name__ == '__main__':
  60.     main()
复制代码

现在有重复检测了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-10 11:37:08 From FishC Mobile | 显示全部楼层
fan1993423 发表于 2018-4-10 10:26
@冬雪雪冬 我当时看题的时候没说要重复啊,只是说输一个数字显示几A几B

举例里有
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 14:24:39 | 显示全部楼层
import random as rd
import math

next = 1


while next:
    countA = 0
    countB = 0

    randNumber = rd.randint(1000,9999)
    randNumber = str(randNumber)
    guessString = input('请猜测四位数字:')
    guessNumber = str(guessString)

    if guessNumber != randNumber:
        worth = (int(randNumber) - int(guessNumber))
        print(worth)
        while abs(worth):
            for each in range(4):
                if randNumber[each] == guessNumber[each]:
                    countA += 1

            for i in range(4):
                for j in range(4):
                    if randNumber[i] == guessNumber[j]:
                        countB += 1
                        break;

            print('没有猜中>_<,猜测情况为:'+ str(countA) + 'A' + str(countB) + 'B')
            
            guessString = input('请猜测四位数字:')
            guessNumber = str(guessString)

            worth = (int(randNumber) - int(guessNumber))
            countA = 0
            countB = 0
            
        print('撒花,猜中啦!\n')

    print('是否要继续游戏,Y为继续,N为不继续')
    x = input()
    if x == 'Y':
        next = 1
    else:
        next = 0
   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-10 14:25:28 | 显示全部楼层
  1. import random as rd
  2. import math

  3. next = 1


  4. while next:
  5.     countA = 0
  6.     countB = 0

  7.     randNumber = rd.randint(1000,9999)
  8.     randNumber = str(randNumber)
  9.     guessString = input('请猜测四位数字:')
  10.     guessNumber = str(guessString)

  11.     if guessNumber != randNumber:
  12.         worth = (int(randNumber) - int(guessNumber))
  13.         print(worth)
  14.         while abs(worth):
  15.             for each in range(4):
  16.                 if randNumber[each] == guessNumber[each]:
  17.                     countA += 1

  18.             for i in range(4):
  19.                 for j in range(4):
  20.                     if randNumber[i] == guessNumber[j]:
  21.                         countB += 1
  22.                         break;

  23.             print('没有猜中>_<,猜测情况为:'+ str(countA) + 'A' + str(countB) + 'B')
  24.             
  25.             guessString = input('请猜测四位数字:')
  26.             guessNumber = str(guessString)

  27.             worth = (int(randNumber) - int(guessNumber))
  28.             countA = 0
  29.             countB = 0
  30.             
  31.         print('撒花,猜中啦!\n')

  32.     print('是否要继续游戏,Y为继续,N为不继续')
  33.     x = input()
  34.     if x == 'Y':
  35.         next = 1
  36.     else:
  37.         next = 0
  38.    
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 23:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表