Pick up an item from a list

0

Good morning, I've been trying to make a mini game, where I enter the words in the WORDS variable and the same type the name randomly, so far Ok. But I wanted to change and I can not do what I want because I can not find a way to do the same. (I started studying programming last weekend). Here is the code:

import random

WORDS = ("palavra1", "palavra2", "palavra3")
word = random.choice(WORDS)
correct = word
champions = ""
while word:
    position = random.randrange(len(word))
    champions += word[position]
    word = word[:position] + word[(position + 1):]
print("")
print("A palavra é:", champions)
guess = input("Nome: ")
while guess != correct and guess != "":
    print("Essa não é a palavra")
    guess = input("A palavra é: ")
if guess == correct:
print("Você acertou\n")

input("\n\nPressione Enter para sair. Versão 1.0")

What I wanted to change, I tried to make the result of the variable "word" pull a hint from a list and insert this hint after the person makes a mistake.

palavra1 = ["dica1","dica2","dica3"]
palavra2 = ["dica1","dica2","dica3"]
palavra3 = ["dica1","dica2","dica3"]

Example:     print ("This is not the word")     print ("Tip:", hint1, ".")

I guess it was confusing, but I think you can understand it.

    
asked by anonymous 23.05.2017 / 13:23

1 answer

1

I will describe the program in parts, as I believe some details can be improved. First, let's create a list of words that will be part of the guessing game. For every word, there will be five tips.

WORDS = [
    ("palavra1", ["dica1", "dica2", "dica3", "dica4", "dica5"]),
    ("palavra2", ["dica1", "dica2", "dica3", "dica4", "dica5"]),
    ("palavra3", ["dica1", "dica2", "dica3", "dica4", "dica5"])
]

To sort a word along with your tips, just use random.choice :

word = random.choice(WORDS)

To create an anagram, that is, to shuffle the letters of the word, you can convert the word to a list, use the sort method and return to a string :

anagram = list(word[0])
anagram.sort()
anagram = "".join(anagram)

print("Tente adivinhar:", anagram)

To read the user's attempts, considering a maximum of 5 chances, just use a for :

# Cinco tentativas:
for i in range(5):

  # Lê a tentativa do usuário:
  guess = input("Tentativa #{}: ".format(i+1))

  # Varifica se o usuário acertou:
  if guess == word[0]: 

    # Sim, exime a mensagem e encerra o loop:
    print("Parabens! Você acertou.")
    break

  # Não, exibe uma dica:
  print("Dica:", word[1].pop(0))

else:
  # O usuário não acertou a palavra:
  print("Ihh, não foi dessa vez.")

To display the hint, I used the pop method, always removing the first hint from the list for the word. In this way, the tips will be given in order: tip 1, tip 2, tip 3, etc. Considering that in the first attempt there will be no tip, for 5 chances only 4 tips are needed.

The complete program would be:

import random

# Lista de palavras e as respectivas dicas:
WORDS = [
    ("palavra1", ["dica1", "dica2", "dica3", "dica4"]),
    ("palavra2", ["dica1", "dica2", "dica3", "dica4"]),
    ("palavra3", ["dica1", "dica2", "dica3", "dica4"])
]

# Sorteia uma palavra:
word = random.choice(WORDS)

# Gera o anagrama da palavra:
anagram = list(word[0])
anagram.sort()
anagram = "".join(anagram)

# Exibe o anagrama:
print("Tente adivinhar:", anagram)

# Cinco tentativas:
for i in range(5):

  # Lê a tentativa do usuário:
  guess = input("Tentativa #{}: ".format(i+1))

  # Varifica se o usuário acertou:
  if guess == word[0]: 

    # Sim, exime a mensagem e encerra o loop:
    print("Parabens! Você acertou.")
    break

  # Não, exibe uma dica:
  print("Dica:", word[1].pop(0))

else:
  # O usuário não acertou a palavra:
  print("Ihh, não foi dessa vez.")
  

See working at Repli.it or Ideone / a>.

    
23.05.2017 / 15:25