为了账号安全,请及时绑定邮箱和手机立即绑定

我的脚本出现语法错误,但我不知道为什么以及如何修复它>

我的脚本出现语法错误,但我不知道为什么以及如何修复它>

小唯快跑啊 2023-07-18 10:07:49
我正在尝试制作一款游戏,用户必须猜测歌曲的名称。他们应该在第一次尝试中得到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")


查看完整回答
反对 回复 2023-07-18
?
DIEA

TA贡献1820条经验 获得超2个赞

而不是使用 else first_guess!=song_name[number[0]]: 尝试仅使用 else.



查看完整回答
反对 回复 2023-07-18
  • 2 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信