首页 > 解决方案 > Python 3.7.3 歌曲猜测代码不一致,随机停止工作

问题描述

我有一个 Python 代码,它是一个歌曲猜谜游戏,你得到了艺术家和歌曲的首字母,我使用了 2 个二维数组,其中一个是艺术家,另一个是歌曲。我知道我应该做一个 3d 数组,但要这么晚才将它更改为我的代码,我必须重组整个代码,而且我没有时间。下面是适当的代码:

                    attemptnum = 5                

                    attempts = 0               
                    for x in range (10):
                        ArtCount = len(artist)
                        print(ArtCount)
                        randNum = int(random.randint(0, ArtCount))
                        randArt = artist[randNum]
                        ArtInd = artist.index(randArt)# catches element position (number)
                        songSel = songs[randNum]
                        print ("The artist is " + randArt)
                        time.sleep(1)

                        print( "The songs first letter be " + songSel[0])
                        time.sleep(1)
                        print("")
                        question = input("What song do you believe it to be? ")
                        if question == (songSel) or ("c"):
                            songs.remove(songSel)
                            artist.remove(randArt)
                            print ("Correct")
                            print ("Next Question")







                        else:
                            attempts = attempts + 1
                            att = attemptnum = attemptnum - attempts
                            print("")
                            print("Wrong,")
                            print (att) and print (" attempts left.")
                            print("")
                            time.sleep(0.5)

                            if attempts == 5:
                                print("GAME OVER")
                                #input leaderboard here
                                print("Exiting in 3 seconds")
                                time.sleep(3)
                                exit()

抱歉,如果我的代码没有那么完善,这是一个学校项目。所以这段代码的作用是随机一个从 0 到艺术家列表有多少元素的数字。然后它使用随机数选择一个艺术家,它使用索引捕获使用的数字,以便可以在歌曲数组上使用它来获取相应的歌曲(我知道很糟糕)它提出问题并显示歌曲的第一个字母. 如果你得到这首歌,你会再次得到相同的代码,但之前的歌曲和艺术家被删除以防止被欺骗。这就是问题所在,当通常运行代码时,它会随机给我一个错误,我试图输出的元素不在列表中:

                    randArt = artist[randNum]
                    IndexError: list index out of range

当你被问到一个问题时,这可能发生在整个代码中的任何地方,你可以进入问题 3 并得到错误,或者甚至没有进入问题 9 并得到错误。这是完全随机的。我觉得它偶尔会尝试获取已被删除的艺术家和歌曲,但这没有意义,因为它只从列表中的数量中选择,而不是原来的 10 个。我不确定我的方式说这是正确的,或者是否有人会理解,因为我肯定不会。澄清一下,我的代码计算列表中有多少元素,使用该数字查找歌曲和艺术家,然后在停止复制后将其删除,但从我所见,它似乎正在尝试查找和元素实际上有多少元素的范围。感谢您接受我的业余代码。

标签: python-3.x

解决方案


random.randint两端都包含。randint(0, 10)将返回范围内的数字

0 <= _ <= 10.

但是 Python 使用基于 0 的索引。

如果li[1, 2, 3]则为len(li)3,但li[3]不存在。

只有li[0],li[1]并且li[2]做。


当你在做

ArtCount = len(artist)
randNum = int(random.randint(0, ArtCount))
randArt = artist[randNum]

您要的是 range 中的一个数字0 <= n <= len(artist)。如果n == len(artist)thenartist[n]将导致IndexError.

顺便说一句,randint返回一个int(因此int在其名称中)。int(randint(...))完全没有必要。

您应该要求在该范围内的随机数0 <= n <= len(artist) - 1或简单地使用random.choice

randArt = random.choice(artist)

您可能想要捕获一个IndexError以防万一artist是一个空列表:

try:
    randArt = random.choice(artist)
except IndexError:
    print('artist list is empty')

推荐阅读