Here is my code:
import random
guesses = 0 #Number of tries to guess.
print ("Welcome to Guess Me!")
print ("\nYou have five guesses, choose letter and guess a word.")
print("\nAll of luck!")
def rand_word():
f = open('sowpods.txt', 'r').readlines() #Sowpods is library with some random words
return random.choice(f).strip()
word = rand_word()
print word #I printed a word just so i can test if is working.
correct = word
lenght = len(word) #I want to tell them how many letters a word contain
new_length = str(lenght)
guess = raw_input("The word is " + new_length + " letters long. Guess a letter: ")
while guesses < 5:
if guess not in word:
print("Sorry, this letter is not in word.")
else:
print("Correct, word contain this letter!")
guesses =+ 1
if guesses > 5:
final_answer = raw_input("You ran out of guesses, what is your answer?")
if final_answer == correct:
print("That's correct, congratulations you won!")
else:
print("Sorry, correct word is" + " " + word + " ,you can try again!")
Problem is when i run my code, and let's say i type letter "s", my sublime freeze and i get message "Sublime 3 is not responding.." and i have to turn it off. Maybe it's while loop? Infinite?
There are a few things.
guesses =+ 1 should be guesses += 1 and at the top of the while loop before any if statements
I think your guess = raw_input should be within the while loop before any of the if statements. So it will keep asking and counting for each guess until it reaches 5. The last if statement within the loop should be if guesses == 5 or guesses >= 5 instead of guesses > 5.
Mostly just reordering things all within the while loop. Everything under the code within the while loop will be ran for every guess.
I changed it according to the above and it works great for me:
while guesses < 5:
guesses += 1
guess = raw_input("The word is " + new_length + " letters long. Guess a letter: ")
if guess not in word:
print("Sorry, this letter is not in word.")
else:
print("Correct, word contain this letter!")
if guesses == 5:
final_answer = raw_input("You ran out of guesses, what is your answer?")
if final_answer == correct:
print("That's correct, congratulations you won!")
else:
print("Sorry, correct word is" + " " + word + " ,you can try again!")
Welcome to Guess Me!
You have five guesses, choose letter and guess a word.
All of luck!
word
The word is 4 letters long. Guess a letter: 1
Sorry, this letter is not in word.
The word is 4 letters long. Guess a letter: 2
Sorry, this letter is not in word.
The word is 4 letters long. Guess a letter: 3
Sorry, this letter is not in word.
The word is 4 letters long. Guess a letter: 4
Sorry, this letter is not in word.
The word is 4 letters long. Guess a letter: 5
Sorry, this letter is not in word.
You ran out of guesses, what is your answer?numbers
Sorry, correct word is word ,you can try again!
You have an infinite loop because your 'guesses' variable is not being inceremented in the while loop; move the 'guesses += 1' into the loop itself e.g.
while guesses < 5:
if guess not in word:
print("Sorry, this letter is not in word.")
else:
print("Correct, word contain this letter!")
guesses += 1
Related
I'm trying to store the guesses so that I can make code for if you have already guessed that letter and also to make code for when they get all of the letters correct. I may be stuck on that part, too, since I don't know how to check if the letters correspond to the letters in the wordname list, especially when it isn't even ordered.
So, I am looking for some code that would store the guesses and also something that can check if all of the correct letters have been guessed. I am thinking it may have something to do with the enumerate() function, but I am not sure since I have never used it before and have only just heard about it.
Also, for your information, the game is hangman if that helps.
wordname = word_choosing()
wordnamelength = len(wordname)
wordnamelist = list(wordname)
def letter_guess1():
sleep(1)
tries = 5
print(f"{oppositeplayer}, the word is {wordnamelength} letters long")
while tries > 0:
guess1 = input("Make your guess: ")
if guess1 in wordnamelist:
print("Congrats, that is correct")
letternum = wordnamelist.index(guess1)
letternum += 1
print(f"This letter is number {letternum}")
elif guess1 not in wordnamelist:
tries -= 1
print(f"You have {tries} left")
if guess1 == "quit".lower():
exit()
return tries
tries = letter_guess1()
I think set is very suitable in this situation. Keep the letters of the word being guessed as a set and add the guess to another set. Then, you can check if the word was guessed correctly by using the intersection of both sets.
Something like that:
wordname = word_choosing()
wordnamelength = len(wordname)
wordnamelist = list(wordname)
def letter_guess1():
sleep(1)
tries = 5
print(f"{oppositeplayer}, the word is {wordnamelength} letters long")
word_set = set(wordname)
guessed_letters = set()
while tries > 0:
guess = input("Make your guess: ")
if guess in guessed_letters:
print("You already guessed this letter")
continue
guessed_letters.add(guess)
if guess in word_set:
print("Congrats, that is correct")
letternum = wordnamelist.index(guess)
letternum += 1
print(f"This letter is number {letternum}")
elif guess not in word_set:
tries -= 1
print(f"You have {tries} left")
if word_set.intersection(guessed_letters) == word_set:
print("You have won!")
if guess == "quit".lower():
exit()
return tries
tries = letter_guess1()
I have made a word guessing game but when it picks a random word with repeating letters in it, it works fine and puts the correctly guessed letters into a list. But how do I make it so it acknowledge that the random word has repeating letters so accepts repeating letters? For example, if the chosen word is ball and you entered s it would display that s is not in the word. If I entered l, it would say l is in the word and add it to a list called correct. If I then entered l again as ball has two l's, it will display that I have already entered that letter and will not let me continue any further. I don't know how I can make it acknowledge that the word has repeating letters. Here is the code:
words = ["burger","chips","ketchup","cake","crisp","coke","fruit"]
correct = []
guessed = []
print("Weclome to guess the word. Aim of the game is to guess all the letters in the word and then type what word you think the word is. The subject is food. You have 15 guesses")
j = random.choice(words)
x = len(j)
count = x
guesses = 15
print("The word is:", x , "letters long.")
while guesses > 0:
guess1 = input("Enter a letter: ").lower()
k = len(guess1)
if k != 1:
print("Not a valid guess!")
if guess1 in j and guess1 in correct:
print("You have already guessed this letter! Your number of guesses left is:", guesses)
if guess1 in j and guess1 not in correct:
print(guess1,"is in the word. You have:", guesses , "guesses left.")
correct.append(guess1)
guesses = guesses-1
count = count-1
print("You have", count ,"of", x , "letters left to guess and you correctly given these letters:", *correct)
if guess1 not in j and guess1 not in guessed:
guessed.append(guess1)
print(guess1, "is not in the word. You have:", guesses , "guesses left and have given the following incorrect letters:", *guessed)
guesses = guesses-1
if guesses == 0:
print("You have ran out of guesses!")
break
if count == 0:
print("Well done! You have guessed all the letters. Now time to de-scramble them! You have unlimited guesses for the word.")
print("These are the following letters you need to de-scramble")
print(*correct)
descramble = input("Enter what you think the word is: ").lower()
if descramble == j:
print("You guessed the word! Well done!")
break
if descramble != j:
print(descramble,"is not the word.")```
You can try this,
import random
words = ["burger","chips","ketchup","cake","crisp","coke","fruit"]
correct = []
guessed = []
print("Weclome to guess the word. Aim of the game is to guess all the letters in the word and then type what word you think the word is. The subject is food. You have 15 guesses")
j = random.choice(words)
x = len(j)
count = x
guesses = 15
already_used_letters = list()
print("The word is:", x , "letters long.")
while guesses > 0:
guess1 = input("Enter a letter: ").lower()
k = len(guess1)
if guess1 in already_used_letters:
print("You already used this letter")
guesses = guesses - 1
already_used_letters.append(guess1)
if k != 1:
print("Not a valid guess!")
if guess1 in j and guess1 in correct:
print("You have already guessed this letter! Your number of guesses left is:", guesses)
if guess1 in j and guess1 not in correct:
print(guess1,"is in the word. You have:", guesses , "guesses left.")
correct.append(guess1)
guesses = guesses-1
count = count-1
print("You have", count ,"of", x , "letters left to guess and you correctly given these letters:", *correct)
if guess1 not in j and guess1 not in guessed:
guessed.append(guess1)
print(guess1, "is not in the word. You have:", guesses , "guesses left and have given the following incorrect letters:", *guessed)
guesses = guesses-1
if guesses == 0:
print("You have ran out of guesses!")
break
if count == 0:
print("Well done! You have guessed all the letters. Now time to de-scramble them! You have unlimited guesses for the word.")
print("These are the following letters you need to de-scramble")
print(*correct)
descramble = input("Enter what you think the word is: ").lower()
if descramble == j:
print("You guessed the word! Well done!")
break
if descramble != j:
print(descramble,"is not the word.")
Try this:
import random
words = ["burger", "chips", "ketchup", "cake", "crisp", "coke", "fruit"]
correctLetters = []
wronglyGuessed = []
print(
"Weclome to guess the word. Aim of the game is to guess all the letters in the word and then type what word you think the word is. The subject is food. You have 15 guesses")
word = random.choice(words)
# word = "aapplle"
lives = 15
while lives > 0:
if len(word) == len(correctLetters):
print(
"Well done! You have guessed all the letters. Now time to de-scramble them! You have unlimited guesses for the word.")
print("These are the following letters you need to de-scramble")
print(*correctLetters)
descramble = input("Enter what you think the word is: ").lower()
if descramble == word:
print("You guessed the word! Well done!")
break
else:
print(descramble, "is not the word.")
guess1 = input("Enter a letter: ").lower()
if len(guess1) > 1 or not guess1.isalpha():
print("Not a valid guess!")
lives -= 1
continue
if guess1 in word:
if word.count(guess1) > correctLetters.count(guess1):
correctLetters.append(guess1)
print("you found a letter")
else:
lives -= 1
print('You guessed all the occurances')
continue
if guess1 not in word:
lives -= 1
print(f'Wrong guess. You have {lives} tries left')
continue
if lives == 0:
print("You ran out of guesses")
Your problem comes from here:
if guess1 in j and guess1 in correct:
print("You have already guessed this letter! Your number of guesses left is:", guesses)
When you use in it will only find the first occurrence of said item and return true. In this case you are interested in also finding the case for duplicates and also keeping track of them. Thus, you need to take another approach: that of finding said duplicates and their number. I used a pre-built method count().
I'd say there are a few more issues with your code:
Use descriptive names for your variables. x, j, k, n can mean a lot of things whereas guestNames is pretty self explanatory
When a case inside one of your ifs was met and all following tests cannot evaluate true, just use a continue statement
Also, on a different note, i'd say that when a letter is guessed you assume all its occurances, but that's not programming related.
I'm trying to create a hangman game. I have a feature that says if the letter that you chose is in a list of all the letters guessed, it will tell you that it has already been guessed. When i run the program it says that the letter has been guessed even though it hasn't been guessed or if it's the first letter that i guessed.
import random
print("---Hangman---")
WordList = ["programming", "computer", "game"]
LetterList = []
Word = random.choice(WordList)
NumberOfLetters = len(Word)
print("There are", NumberOfLetters, "letters in the word.")
for i in range (NumberOfLetters):
print("Guess a letter:")
LetterGuess = input()
LetterList.append(LetterGuess)
if LetterGuess in LetterList:
print("You already guessed that letter")
elif LetterGuess in Word:
print("Correct")
print("You now have the correct letters: ", LetterList)
i += 1
elif LetterGuess not in Word:
print("Wrong")
print("You have the correct letters: ", LetterList)
When i guess a number it says this:
---Hangman---
There are 11 letters in the word.
Guess a letter:
d
You already guessed that letter
You add the guess to the list, then immediately check whether it is there.
LetterList.append(LetterGuess)
if LetterGuess in LetterList:
I am very new to python and I am attempting to make a hangman game.
I would like to change a string to show the number of guessed letters but for some reason I keep on getting weird results. Here is my code:
import random
guesses_left = 9
def show_guesses_left():
print("You have", guesses_left, "guesses left")
wordlist = ['nerd', 'python', 'great', 'happy', 'programmer', 'long', 'short', 'stupid']
word = random.choice(wordlist)
wordwin = word
hidden_word = ["?" for q in word]
letters_guessed = ''.join(hidden_word)
print("Welcome to Hangman!!")
print("My word is", len(word), "letters long")
print(wordwin)
print(letters_guessed)
def request_guess():
global guesses_left
global word
global letters_guessed
x = input(f"What is your guess? \n{letters_guessed}")
if x in word:
print("Great you guessed a letter")
t = word.find(x)
word = word.replace(x, "")
print(t)
letters_guessed = letters_guessed[:t] + letters_guessed[t:t+1].replace('?', x) + letters_guessed[t+1:]
elif type(x) is not str or len(x) > 1:
print("Invalid guess, Your guess must be 1 letter long")
else:
print("Wrong!")
guesses_left -= 1
show_guesses_left()
def start_game():
global letters_guessed
global word
global guesses_left
letters_guessed = ''.join(hidden_word)
while True:
if guesses_left > 0 and len(word) != 0:
request_guess()
elif len(word) == 0:
print(f"YOU WIN!!!, the word was {wordwin}")
break
else:
print("You lose! Better luck next time!")
break
start_game()
I keep on getting this result where it only works for the for some letters and the placing is wrong. Here is my result:
Welcome to Hangman!!
My word is 4 letters long
long
????
What is your guess?
????l
Great you guessed a letter
0
What is your guess?
l???n
Great you guessed a letter
1
What is your guess?
ln??o
Great you guessed a letter
0
What is your guess?
ln??g
Great you guessed a letter
0
YOU WIN!!!, the word was long
Why cant i just slice the string change one character and slice the rest?
Why does it work the first time and not the second?
If anybody can explain to me what is going on it would be appreciated
Solution
import random
guesses_left = 9
def show_guesses_left():
print("You have", guesses_left, "guesses left")
wordlist = ['nerd', 'python', 'great', 'long', 'short', 'stupid', 'happy', 'programmer']
word = random.choice(wordlist)
wordwin = list(word)
hidden_word = list('?' * len(word))
letters_guessed = ''.join(hidden_word)
print("Welcome to Hangman!!")
print("My word is", len(word), "letters long")
print(letters_guessed)
def request_guess():
global guesses_left
global word
global letters_guessed
x = input("\nWhat is your guess?\n" + letters_guessed + "\n")
if x in word:
print("\nGreat you guessed a letter")
for i, j in enumerate(word):
if j == x:
hidden_word[i] = j
letters_guessed = ''.join(hidden_word)
print(letters_guessed + "\n")
elif type(x) is not str or len(x) > 1:
print("Invalid guess, Your guess must be 1 letter long")
else:
print("\nWrong!")
guesses_left -= 1
show_guesses_left()
def start_game():
global letters_guessed
global word
global guesses_left
letters_guessed = ''.join(hidden_word)
while True:
if guesses_left > 0 and letters_guessed != word:
request_guess()
elif letters_guessed == word:
print("YOU WIN!!!, the word was " + word)
break
else:
print("\nYou lose! Better luck next time!")
break
start_game()
Notes
Got it to work!
Sorry short on time will be back to help more, but take a look around. I used some different methods than you originally had, seems like there was a issue with your letters_guessed not revealing letters past a letter already guessed. This will work for double letters as well, which also seemed to be an issued with your original code.
Again sorry, will be back to explain more!
The main problem is the code you use to get the index of the guessed letter:
t = word.find(x)
word = word.replace(x, "")
This shortens word after each correct guess, so t will not be the desired value after the first correct guess.
However, even if you fix this, you will still not properly handle the case that the guessed letter occurs multiple times.
Here is a short example that shows how to solve both problems:
answer = 'long'
hidden = '?' * len(answer)
print("Welcome to hangman!")
while True:
guess = input("Guess a letter: ")
result = ''
if guess in answer:
for answer_letter, hidden_letter in zip(answer, hidden):
if guess == answer_letter:
result += guess
else:
result += hidden_letter
hidden = result
print(hidden)
if hidden == answer:
print("You guessed it!")
break
The main problem is that you are basing that splicing on a variable that you modify on the go. In particular, the variable word, where you look for the position of x, the guessed letter. The position is correct until word gets modified in length, as you replace the letter with an empty string.
An easy fix for that is to simply change the replace statement, and put either an empty space, or another character, that the user would not normally put. In my example, I would replace:
word = word.replace(x, "")
with
word = word.replace(x, " ")
That of course breaks the program exit logic: you can never win.
There is still another issue, that is multiple occurrences of the same letter are not accounted properly. In fact, the program loops until exhaustion, when multiple instances of the same letter are in word.
That is due to the fact that find will only reveal the position of the first instance of a given letter, and you don't account for possible duplicates.
There are several ways to fix that, but I think the main issue is identified.
For an alternate implementation, check https://eval.in/1051220
I am suppose to create a game where the computer picks a random word and the player has to guess that word. The computer will then tell the player how many letters are in the word. Then the player gets five chances to ask whether a letter is in the word. The computer can only respond with "yes" or "no". Then, the player must guess the word.
Somehow, the program that I wrote below is not accurately giving the player 5 chances to ask the whether the letter is in the word before letting the player guess the word. May I know what went wrong? Thank you!
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
tries = 0
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
The two while loops one after the other are not correct logic (the indentation as you report it is also broken but I'm guessing that's just a problem of your copy-and-paste here, not of your code itself, or else you'd be getting syntax errors:-).
Suppose for example that the first time the player picks a guess_letter that's not in the word: then the first while exits immediately and will never be entered again!
You need a single loop, with its iteration independent of whether guess_letter is or isn't in the word -- only what is printed should depend on that check! A for loop may be more readable but you can perfectly well do it with a while if you prefer -- but it would be something like:
while tries < 5:
tries += 1
guess_letter = raw_input('Guess another letter in the word.')
if guess_letter in word:
print 'yes'
else:
print 'no'
print 'Please guess the word.'
with the initialization before it and the last guess and check after it.
I also see you want to treat the first guess differently (specifically, using a different prompt). That's probably best achieved by using a variable within the prompt...:
tries = 0
art = 'a'
while tries < 5:
tries += 1
guess_letter = raw_input('Guess ' + art + ' letter in the word.')
art = 'another'
if guess_letter in word:
print 'yes'
else:
print 'no'
print 'Please guess the word.'
The following works. Its sloppy and I don't like it much but I think your biggest issue could have been indentation. After that I added a while loop to nest your two other while loops.
If you want to change something to clean it up I would suggest one while loop (while tries < 4) and then replace the original while loops with if else statements.
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
while tries < 4:
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
break
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
break
cleaned up version:
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
tries = 0
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
while tries < 4:
tries +=1
guess_letter = raw_input("Guess a letter in the word.")
if guess_letter in word:
print "Yes"
else:
print "No"
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
Your code seems too complicated. I advise you to use a for loop, which fits to your problem. Here is a lightweight version of your game (tested on Python 3.4):
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
print("There are {} letters in the word.".format(len(word)),
"You have 5 chances to guess a letter in the word.",
"After which you will be required to guess the word.")
for _ in range(5):
guess_letter = input("Guess a letter in the word: ")
if guess_letter in word:
print("YES")
else:
print("NO")
answer = input("Guess the word: ")
if answer == word:
print("That is correct!")
else:
print("Game over!")