在循环中创建列表

时间:2020-04-01 13:42:17

标签: python list loops dictionary

我创建了一个“策划”风格的游戏,用户可以在其中猜测计算机生成的确切数字顺序。

我想添加一个功能,用于存储先前的猜测,以便可以在进行下一个猜测之前将其重播给用户

在我的脑海中,以当前的程序布局,我认为如果可以创建一系列“ pul”(先前的用户列表),那将是很好的。

例如,随着while循环的运行和“ count”变量的增加,列表将被适当地命名。

cl,计算机列表

ul,用户列表

ol,输出列表

脉冲,以前的用户列表

当count = 0时,用户输入将创建ul = [1、3、4、8]

pul_0 = ul

'#so pul_0 = [1、3、4、8]

当count = 1时,用户将为ul = [2,2,9,0]创建新列表

pul_1 = ul

'#so pul_1 = [2,2,9,0]

'#,当然pul_0仍为[1、3、4、8]

任何帮助表示感谢,谢谢。

如果需要的话,这里有一些代码可以提供更多的细节,底部的完整程序可以提供更多的上下文。

n = int(input("how many numbers to guess? (normally 4)"))

att = int(input("how many attempts? (normally 10)"))

count = 0
while count < att:

    ul = [int(input()) for i in range(n)]

    pul(count) = ul

    count = count + 1

我已经阅读了一些有关使用字典来解决此问题的信息,但没有让它们为我工作 create lists of unique names in a for -loop in python

{'pul_{}'.format(count):[] for i in ul}

下面的完整程序


n = int(input("how many numbers to guess? (normally 4)"))

att = int(input("how many attempts? (normally 10)"))

cl = [randint(0, 9) for i in range(n)]

wincount = 0
count = 0

while count < att:

    print(att-count, "attempts remaining. Please enter",n, "numbers, pressing enter between each")

    ul = [int(input()) for i in range(n)]



    wincount = 0
    nearcount = 0
    for i in range (n):

        if cl[i] == ul[i]:
            wincount = wincount + 1

        for j in range (n):
            if cl[i] == ul[j]:
                nearcount += 1

    nearcount = nearcount - wincount

    if wincount == n:
        print("winner")
        count = att + 1

    if wincount != n:
        print("you have", wincount,"correct numbers in the correct positions")
        print("you have a further", nearcount,"correct numbers, but they are in the wrong positions")
        count = count + 1




if count == att:

    print("no more attempts, game over")
    print("btw the correct answer was", cl)
elif count == att + 1:
    print("nice 1")


'''



I have read a small amount about using a dictionary to try and solve this issue, but i played around with it a little and didn't get very far.

Thanks in advance!

1 个答案:

答案 0 :(得分:0)

您的代码已修改

stream()