I am trying to make it where if you have word "man" for example it will look like this _ _ _. If the user types in "m" it will look like m _ _. I know my issue lays in the "# Where user will type guess" comment under the for loop
import random
user_input = ""
turns = 5
# List of words
print("Welcome to Advanced Hang Man!")
guesses = ["hello"]
# Picks a random word from the list and prints the length of it
random_guesses = (random.choice(guesses))
right_guess = []
wrong_guess = []
# Prints the hidden word in "_" format
hidden_word = "_" * len(random_guesses)
print(hidden_word)
# Where user will type guess
while True:
user_input = input("Please enter a letter once at a time:")
user_input = user_input.lower()
for i in range(len(random_guesses)):
if user_input == random_guesses[i]:
print(hidden_words)
You need to iterate over actual word and check for the character in the right_guesses list. If not found, replace the character with _ in new word. Below is the sample code to achieve this:
>>> my_word = "StackOverflow"
>>> right_guesses = ['s', 'o', 'c']
>>> ' '.join([word if word.lower() in right_guesses else '_' for word in my_word])
'S _ _ c _ O _ _ _ _ _ o _'
Here is my attempt at making the game. I'm not using your right_guesses list nor am i using the wrong-guesses but it has the basic funtionality of a hang man game:
user_input = ""
print("Welcome to Advanced Hang Man!")
random_word= 'bicycle'
hidden_word = "_" * len(random_word)
going = True
while going:
print(hidden_word)
user_input = input("Please enter a letter once at a time:");
user_input = user_input.lower()
for i in range(len(random_word)):
if user_input == random_word[i]:
print ('Letter found!')
temp = list(hidden_word)
temp[i] = user_input
hidden_word = ''.join(temp)
if (hidden_word == random_word):
print ('You Won!!! The word was ' + random_word)
going = False
Related
def read_input(random_words):
word = choose_random_words()
rand = random.randrange(0, 3)
letter = find_row_letter(word[rand])
print(rand)
user_input = timed_input("Input: %s " % letter)
return user_input
This function chooses a random word out of a list. This Word is then linked to a letter of the alphabet. You can see the timed_input as a normal input, as it is not important for my question.
def handle_input(random_words):
letter = read_input(random_words)
word =
print(letter)
if word == letter:
print("Correct")
else:
print("False")
In this function I want to compare if the user input from function read_input is the same as the chosen word from the same function.
Make read_input return the word too
def read_input(random_words):
word = choose_random_words()
rand = random.randrange(0, 3)
letter = find_row_letter(word[rand])
user_input = timed_input("Input: %s " % letter)
return user_input, word
def handle_input(random_words):
letter, word = read_input(random_words)
What you can do is declare the word variable to be global:
def read_input(random_words):
global word
word = choose_random_words()
rand = random.randrange(0, 3)
letter = find_row_letter(word[rand])
print(rand)
user_input = timed_input("Input: %s " % letter)
return user_input
That allows you to access the word variable from anywhere.
I'm writing a hangman game and found a code online for replacing blank spaces with the letters after a correct guess was made but it unfortunately produces a 'NoneType' error
I'm not sure how to correct it and I haven't been able to apply other answers I've found online to the code to fix it
word_list = ['around','august','branch','bright','castle','change','across']
tries = 0
word = random.choice(word_list)
print(word)
blank = print ("_ "*len(word))
while tries < 7:
guess = input("Enter a letter: ")
blank = list(blank)
for k in range(0, len(word)) or []:
if guess == word[k]:
blank[k] = guess
print (''.join(blank))
You are storing the return of print and not the value in print into blank
Try this :
import random
word_list = ['around','august','branch','bright','castle','change','across']
tries = 0
word = random.choice(word_list)
print(word)
blank = "_ "*len(word) #storing what you want in variable
print(blank) #print the variable
while tries < 7:
guess = input("Enter a letter: ")
blank = list(blank) #here you do list() on the variable and not the return of print >> OK
for k in range(0, len(word)) or []:
if guess == word[k]:
blank[k] = guess
print (''.join(blank))
This is my code so far for getting the string of the 1st letters of each word in a string but when I return it to main it does not print anything.
def phrases(string):
result2 = ''
for letter in string.split():
result2 += letter[0]
return result2
def main():
string = input('Please enter a string: ')
replacePhrases = input('Do you want to replace phrases? ')
if replacePhrases == 'y' or replacePhrases == 'Y':
result2 = phrases(string)
print(result2)
Hey guys so I have my program working to a certain extent. My program is suppose to check if there is an "A" in the user input and if done so it will swap that "A" with the next letter.
Here are the examples:
"tan" = "TNA"
"abracadabra" = "BARCADABARA"
"whoa" = "WHOA"
"aardvark" = "ARADVRAK"
"eggs" = "EGGS"
"a" = "A"
In my case this is what works and doesn't work:
Works:
tan to TNA
Doesn't work:
abracadabra = BARCADABAR
whoa = WHO
aardvark = ARADVRA
eggs = EGG
a =
a just equals nothing.
What I'm getting at is that the last character isn't printing and I'm not sure how to do so.
def scrambleWord(userInput):
count = 0
Word_ = ""
firstLetter_ = ""
secondLetter_ = ""
while count < len(userInput):
if count+1 >=len(userInput):
break #copy last character
firstLetter_ = userInput[count] #assigning first letter
secondLetter_ = userInput[count+1] #assigning next letter
if firstLetter_ == 'A' and secondLetter_ != 'A':
Word_ += (secondLetter_ + firstLetter_) #Swap then add both letters
count+=1
else:
Word_+=firstLetter_
count+=1
return Word_
def main():
userInput = input("Enter a word: ")
finish = scrambleWord(userInput.upper())
print(finish)
main()
Probably because you are just breaking without writing the userinput[count] into the word.
if count+1 >=len(userInput):
Word_ += userInput[count]
break #copy last character
This should help
I'm very new to python and I'm trying to make a hangman game. I currently have a line of code saying word = (random.choice(open("Level1py.txt").readline())).
I'm getting the error 'str' object does not support item assignment.
Here is the rest of my code (sorry for the mess):
import random
def checkLetter(letter, word, guess_word):
for c in word:
if c == letter:
guess_word[word.index(c)] = c
word[word.index(c)] = '*'
print(guess_word)
word = (random.choice(open("Level1py.txt").readline().split()))
guess_word = ['_' for x in word]
print(guess_word)
while '_' in guess_word:
guess = input('Letter: ')
print(checkLetter(guess, word, guess_word))
Strings are immutable in python. An easy workaround is to use lists, which are mutable:
st = "hello"
ls = list(st)
ls[3] = 'r'
st = ''.join(ls)
print(st)
Outputs
helro
Edit: here's how you would implement it in your own code
import random
def checkLetter(letter, word, guess_word):
for c in word:
if c == letter:
guess_word[word.index(c)] = c
word_list = list(word)
word_list[word.index(c)] = "*"
word = ''.join(word_list)
print(guess_word)
word = 'test'
guess_word = ['_' for x in word]
print(guess_word)
while '_' in guess_word:
guess = input('Letter: ')
print(checkLetter(guess, word, guess_word))
Note that there are still other issues that have nothing to do with this, like printing None and duplicate printing
You can also solve your problem using a dictionary:
word = "my string" #replace this with your random word
guess_word = {i: '_' for i in set(word)} # initially assign _ to all unique letters
guess_word[' '] = ' ' # exclude white space from the game
wrong_attempts = 0
while '_' in guess_word.values():
guess = input('Letter: ')
if guess in guess_word.keys():
guess_word[guess] = guess
else:
wrong_attempts += 1
if wrong_attempts > 11:
break
printable = [guess_word[i] for i in word]
print(' '.join(printable))
if '_' in guess_word.values():
print('you lost')
else:
print('congratulation, you won')