繁体   English   中英

我如何解决我在 python 中的元组的问题

[英]how do i fix my problem with my tuple in python

我在 python 中有一个问题,有人告诉我,这与代码中的元组有关,登录后空闲给了我这个错误

回溯(最近一次通话最后):

artist, song = choice.split()

ValueError:需要超过 1 个值才能解压

这是我的完整代码

import random
import time

x = 0
print("welcome to music game please login below")

AuthUsers = {"drew":"pw","masif":"pw","ishy":"pw"}
#authentication
PWLoop = True

while PWLoop:
    userName = input("what is your username?")
#asking for password
    password = (input("what is the password?"))

    if userName in AuthUsers:
        if AuthUsers.get(userName) == password:
            print("you are allowed to play")
            PWLoop = False
        else:
            print("invalid password")
    else:
        print("invalid username")


#GAME

#SETTING SCORE VARIBLE
score = 0

#READING SONGS

read = open("SONGS.txt", "r")
songs = read.readline()
songlist = []

for i in range(len(songs)):
    songlist.append(songs[i].strip())



while x == 0:
    #RANDOMLY CHOSING A SONG

    choice = random.choice(songlist)
    artist, song = choice.split()


#SPLITTING INTO FIRST WORDS

songs = song.split()
letters = [word[0] for word in songs]


#LOOp

for x in range(0,2):
    print(artist, "".join(letters))
    guess= str(input(Fore.RED + "guess the song"))
    if guess == song:
        if x == 0:
            score = score + 2
            break
        if x == 1:
            score = score + 1
            break


#printing score

问题是由于这两行:

choice = random.choice(songlist)
# choice will be single item from songlist chosen randomly.
artist, song = choice.split() # trying to unpack list of 2 item
# choice.split() -> it will split that item by space
# So choice must be a string with exact one `space`
# i.e every entry in songlist must be string with exact one `space`

作为您的文件格式https://pastebin.com/DNLSGPzd

为了解决这个问题,只需拆分,

更新代码:

artist, song = choice.split(',')

artist, song = choice.split()更改为:

song, artist = choice.split(',')

这将解决您的问题。 根据您提供的数据,您应该使用, .\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM