![](/img/trans.png)
[英]How do i make my assistant respond to a preset response to “how are you” in python code after asking them how they are
[英]How do I add a certain amount of attempts to my code after asking a question?
我有一個代碼可以選擇歌手的隨機歌曲,但是只顯示首字母,並且用戶必須猜測給定歌手的歌曲名稱,我需要添加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
您可以使用變量來計算嘗試次數。 並將其添加到while循環中。 像這樣的東西:
count = 0
while count<2:
answer = input('enter the song name: ')
if answer == solution:
<move on to next question>
else:
count +=1
我已經編寫了一個獨立的代碼,在該代碼中,如果知道藝術家,並且所選擇的歌曲是已知的,則將嘗試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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.