Reputation: 73
I'm creating a "guess the word" game for my intro to programming class. It uses parallel tuples where one list is the random words & the second list is the corresponding hints for those words. The game is supposed to print one of the random words with underscores in place of the vowels (ex: J_p_n instead of Japan) and the user guesses the word based off of that & the hint. With my current code, it doesn't print the word without the vowels but instead just prints a single letter. How do I fix this?
import random
#Parallel tuples
guesswords = ("Japan","France","Mexico","Italy")
guesshints = ("Sushi comes from here","Croissants come from here","Tacos come from here","Pizza comes from here")
#Variables
new_words = ""
vowels = "AaEeIiOoUu"
#Random
index = random.randrange(len(guesswords))
guesses = 5
#Replacing vowels
for letter in guesswords:
if letter not in vowels:
new_words += letter
else:
new_words += "_"
#Output
print(new_words[index].center(80, " "))
print("Hint:",guesshints[index])
while guesses > 0:
input_string = "\nGuess the word! You have " + str(guesses) + " guesses remaining: "
user_guess = input(input_string)
if user_guess.upper() == guesswords[index].upper():
print("YOU WIN")
break
guesses -= 1
print("GAME OVER")
Upvotes: 1
Views: 462
Reputation: 2812
Using a regex is probably the easiest solution:
import re
original_word = 'America'
vowels = re.compile(r'[aeiou]', re.IGNORECASE)
with_underscores = re.sub(vowels, '_', original_word)
print with_underscores
Result:
_m_r_c_
Upvotes: 1
Reputation: 2293
Change for letter in guesswords:
to for letter in guesswords[index]:
. And remove [index]
from this line print(new_words[index].center(80, " "))
Upvotes: 0
Reputation: 2036
Instead of reconstructing the word, you could just try something like this
for v in vowels:
word = word.replace(v,'_')
Upvotes: 0