簡體   English   中英

我的腳本出現語法錯誤,但我不知道為什么以及如何修復它>

[英]My script comes up with syntax error but I can't figure out why and how to fix it>

我正在嘗試制作一個用戶必須猜測歌曲名稱的游戲。 他們應該在第一次嘗試中得到 3 分。

players_score = 0
first_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 + 3
else first_guess!=song_name[number[0]]:
print("Try again")

這是我對代碼的評分部分,出於某種原因就行了

else first_guess!=song_name[number[0]]:

它作為語法錯誤出現,我不知道為什么以及如何修復它。

這是迄今為止的全部代碼以供參考:

# welcoming them to the game
print("Hi, welcome to the game. Before we start, please enter your details so we can verify you are eligible to play. Thanks!")

# input age
password = int(input("Enter password: "))

#checking if the password
if password==1234:
    print("Enjoy the game!")
else:
    print("Try again")
    
    
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.officialcharts.com/charts/uk-top-40-singles-chart/'

#opening connection, grabbing page
uClient = uReq (my_url)
page_html = uClient.read()
uClient.close()

#html parsing
page_soup = soup(page_html, "html.parser")
#print (page_soup.p)
 
#grabs each section
containers = page_soup.findAll('div' ,{"class":"title-artist"})

spans = page_soup.findAll('span' ,{"class":"position"})  

# isolates song artist and name
a_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 random
exampleList = [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_list2
random_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)
  
players_score = 0
first_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 + 3
else first_guess!=song_name[number[0]]:
print("Try again")
    ```

您需要將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")

而不是使用else first_guess!=song_name[number[0]]:嘗試只使用else

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM