提出问题后如何在代码中添加一定数量的尝试?

时间:2019-04-30 09:30:52

标签: python

我有一个代码可以从歌手那里随机挑选一首歌曲,但是只显示首字母,用户必须猜测给定歌手的歌曲名称,我需要添加2次尝试,如果他们弄错了,我结束代码,但是如果他们做对了,他们将继续下一个问题。

f1 = open("Eminem.txt", "r")            #Opens external notepad file and the read mode ("r")
f2 = open("UnlikePluto.txt", "r")        #Opens external notepad file and the read mode ("r")
f3 = open("Marshmello.txt", "r")        #Opens external notepad file and the read mode ("r")
f4 = open("Eminem1.txt", "r")            #Opens external notepad file and the read mode ("r")
f5 = open("UnlikePluto1.txt", "r")        #Opens external notepad file and the read mode ("r")
f6 = open("Marshmello1.txt", "r")        #Opens external notepad file and the read mode ("r")
f7 = open("Eminem2.txt", "r")            #Opens external notepad file and the read mode ("r")
f8 = open("UnlikePluto2.txt", "r")        #Opens external notepad file and the read mode ("r")
f9 = open("Marshmello2.txt", "r")        #Opens external notepad file and the read mode ("r")

我已经将歌手姓名,歌曲名称和实际问题保存在一个外部文件中,但是我不知道如何通过2次猜测歌曲来编码问题。谢谢:D

2 个答案:

答案 0 :(得分:2)

您可以使用变量来计算尝试次数。并将其添加到while循环中。像这样的东西:

count = 0
while count<2:
    answer = input('enter the song name: ')
    if answer == solution:
        <move on to next question>
    else:
        count +=1

答案 1 :(得分:0)

我已经编写了一个独立的代码,在该代码中,如果已知艺术家,并且所选择的歌曲是已知的,则将尝试3次,并相应显示正确或错误的答案。您可以根据需要扩展它!

def choose_song(artist, songs, song_choosen):

    idx = 0
    guess_correct = False
    song = ''

    while idx < 3:

        guess = input('The artists is {} and the song is {}. Enter your guess {}>>'.format(artist, song_choosen[:3], idx+1))

        #Iterate through all songs to check if the guess is right, if it is, break out of for loop
        for song in songs:
            if guess.lower().strip() == song.lower().strip():
                guess_correct = True
                song = guess
                break
        #If the user guessed correctly, we are done else try again
        if guess_correct:
            break
        else:
            idx+= 1

    #Show the final output accordingly
    if guess_correct:
        print('You guessed correctly. The song is indeed {}'.format(song))
    else:
        print('You guessed wrong. The song is {}'.format(song_choosen))


choose_song('Eminem', ['Rap God', 'Slim Shady'], 'Rap God')

输出将为

The artists is Eminem and the song is Rap. Enter your guess 1>>Rap King
The artists is Eminem and the song is Rap. Enter your guess 2>>Rap Dog
The artists is Eminem and the song is Rap. Enter your guess 3>>Rap Kid
You guessed wrong. The song is Rap God

The artists is Eminem and the song is Rap. Enter your guess 1>>Rap God
You guessed correctly. The song is indeed Rap God

The artists is Eminem and the song is Rap. Enter your guess 1>>Rap Dog
The artists is Eminem and the song is Rap. Enter your guess 2>>rap god
You guessed correctly. The song is indeed rap god

要在此基础上构建,只需执行此操作即可。

import random

#Your dictionary of artists to songs list
dct = {'Eminem': ['Rap God', 'Slim Shady'],
       'Marshmello': ['Happier', 'Friends'],
       'Pearl Jam': ['Even Flow', 'Black']}

import random
#Iterate through all artists and pick a song at random
for key, value in dct.items():
    choose_song(key, value, random.choice(value))

输出可能看起来像

The artists is Eminem and the song is Rap. Enter your guess 1>>rap god
You guessed correctly. The song is indeed rap god
The artists is Marshmello and the song is Hap. Enter your guess 1>>Happier
You guessed correctly. The song is indeed Happier
The artists is Pearl Jam and the song is Bla. Enter your guess 1>>Black
You guessed correctly. The song is indeed Black