所以,我正在为一个初学者编码课做一个Hangman游戏,现在我有一个函数,它会从一个选定的类别中返回一个随机单词。接下来,我想制作一个'*'数组,每个*代表单词中的一个字母。
我希望你帮我修复的功能是Array_Juego(palabra_a_jugar)
__author__ = 'Rodrigo Cano'
#Modulos
import random
#Variables Globales
intentos = 0
incorrectos = 0
palabras = [(1,"disclosure"),(1,"thenational"),(1,"foals"),(1,"skepta"),(1,"shamir"),(1,"kanye"),
(1,"fidlar"),(1,"lcdsoundsystem"),(1,"lorde"),(1,"fkatwigs"),(1,"miguel"),(1,"rtj"),
(1,"boniver"),(1,"strokes"),(2,"vaporwave"),(2,"witchouse"),(2,"shoegaze"),(2,"memerap"),
(2,"paulblartisoursaviour"),(3,"glockenspiel"),(3,"aesthetic"),(3,"schadenfreude"),
(3,"bonvivant"),(3,"swag"),(3,"jue")]
palabra_a_jugar = ''
#Funciones
def Definir_Palabra():
eleccion = int(input("Bienvenido, que categoria quiere usar:"
'\n'"1 - Musica que Escuche Mientras Lo Hacia"
'\n'"2 - Generos Musicales"
'\n'"3 - Palabras Pretenciosas"))
palabras_escogidas = [i for i in palabras if eleccion in i ]
palabra_a_jugar = str(palabras_escogidas[random.randint(0,len(palabras_escogidas))].__getitem__(1))
return palabra_a_jugar
def Array_Juego(palabra_a_jugar):
Array_Palabra =[]
for palabra_a_jugar in range(0,len(palabra_a_jugar.split())):
Array_Palabra.append("_")
return Array_Palabra
print(Definir_Palabra())
print(Array_Juego(palabra_a_jugar))
答案 0 :(得分:1)
问题出在这一行:
for palabra_a_jugar in range(0,len(palabra_a_jugar.split())):
Array_Palabra.append("_")
palabra_a_jugar
以字符串开头。 split()
返回一个子字符串列表,在分隔符处断开,默认情况下为空格。所以
'palabara a jugar'.split()
会返回['palabra', 'a', 'jugar']
然而,大概你只有一个字。假设palaba_a_jugar
的值是“pretenciosas”。然后没有空格,所以
'palabara a jugar'.split()
将返回
['pretenciosas']
单元素列表。这解释了您所看到的行为。
然而,你不需要像这样在谷仓周围,并不值得为它编写功能。与palabra_a_jugar
长度相同的星标列表只是
len(palabra_a_jugar) * ['*']
For example, 4*['*']== ['*','*','*','*']