我正在嘗試制作一款游戲,用戶必須猜測歌曲的名稱。他們應該在第一次嘗試中得到3分。players_score = 0first_guess = input ('Enter your first guess: ')if first_guess==song_name[number[0]]: print("Well done! You've earned 3 points") players_score= players_score + 3else first_guess!=song_name[number[0]]:print("Try again")這是我的評分部分代碼,出于某種原因就行了else first_guess!=song_name[number[0]]:它出現為語法錯誤,我不知道為什么以及如何修復它。這是到目前為止的整個代碼供參考:# welcoming them to the gameprint("Hi, welcome to the game. Before we start, please enter your details so we can verify you are eligible to play. Thanks!")# input agepassword = int(input("Enter password: "))#checking if the passwordif password==1234: print("Enjoy the game!")else: print("Try again") from urllib.request import urlopen as uReqfrom bs4 import BeautifulSoup as soupmy_url = 'https://www.officialcharts.com/charts/uk-top-40-singles-chart/'#opening connection, grabbing pageuClient = uReq (my_url)page_html = uClient.read()uClient.close()#html parsingpage_soup = soup(page_html, "html.parser")#print (page_soup.p) #grabs each sectioncontainers = page_soup.findAll('div' ,{"class":"title-artist"})spans = page_soup.findAll('span' ,{"class":"position"}) # isolates song artist and namea_tags = [container.findAll('a') for container in containers]song_name = [i[0].text for i in a_tags]song_artist = [i[1].text for i in a_tags]import randomexampleList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]sampled_list2 = random.sample(exampleList, 1)number = sampled_list2random_song_name = song_name[number[0]]random_song_artist = song_artist[number[0]] meme = WAP for part in number: print(random_song_name[0].upper() + ". ", end="") print() print (random_song_artist)
2 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
您需要更改else為elif并在下一行添加縮進,如下所示:
if first_guess==song_name[number[0]]:
print("Well done! You've earned 3 points")
players_score= players_score + 3
elif first_guess!=song_name[number[0]]:
print("Try again")
或者您可以添加else這將在執行相同操作的同時保持更清晰的代碼:
if first_guess==song_name[number[0]]:
print("Well done! You've earned 3 points")
players_score= players_score + 3
else:
print("Try again")
添加回答
舉報
0/150
提交
取消