Pick an item from a list

Asked

Viewed 1,130 times

0

Good morning, I’ve been trying to make a mini game, where I insert the words in the variable WORDS and the same type the name randomly, so far Ok. But I wanted to change and I’m not able to do what I want, because I can’t find a way to do the same (I started studying programming last weekend). Follows 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 has made a mistake.

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

Example: print("That’s not the word") print("Tip:", dica1,".")

I imagine you got confused, but I think you can understand.

  • WORDS defines a list of words that will be drawn. word is the word drawn. champions is an anagram of word. The value is displayed champions, so that the user tries to get the word right, correct. The program keeps ordering a new word until the user gets it right. Now you want a hint to appear when the user gets it wrong. Question: Each diva should be displayed only once or they can repeat themselves and always show a hint when the user misses the word?

  • Always show a new tip when it misses, I put a repeat, but I did not put here in the code of the site, the person will have five chances, if you miss the five, lose.

  • And there will be five tips?

  • Yes, there will be five tips, I put only three as an example, but there will be five.

1 answer

2


I will describe the programme 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 each 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 draw a word, along with your tips, just use random.choice:

word = random.choice(WORDS)

To create an anagram, that is, shuffle the letters of the word, you can convert the word to a list, use the method sort 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 tip, I used the method pop, always removing the first hint from the list referring to the word. This way the tips will be given in order: tip 1, tip 2, tip 3, etc. Whereas on the first try there will not be a hint, for 5 chances are required only 4 tips.

The whole 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 on Repli.it or in the Ideone.

  • Thanks Anderson, I’ll use your tips. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.