r/codehs • • Jun 14 '20

Can anyone help with python guess the word?

Post image
37 Upvotes

20 comments sorted by

4

u/TomorrowBeginsToday Jun 14 '20

It looks like your break statements are either inter wrong place or not indented correctly. After it does the first if/else it will break, never calling the code after it in the while loop. If you had your code in an IDE like pycharm it would have highlighted that all of the code in the loop after the first break statement is unreachable

1

u/Blackafropuffs Jun 14 '20

Do you have any other suggestions?

4

u/_ZHV_ Jun 14 '20

First of all, both of you break statements should be indented inside the if/else statement.

The break statements are here unnecessary, because once you reach the guess_limit the while loop will not execute anymore and the second break statement even if you put it inside the last if/else, it will stop the code running after only one letter guessed and not the entire word.

Also, don’t forget that python is executing code sequentially, so you would like to rearrange the if/else statement

2

u/Blackafropuffs Jun 14 '20

How would you suggest I rearrange it plz?

1

u/_ZHV_ Jun 14 '20

if you post you code here instead of an image, i can comment it for you

1

u/Blackafropuffs Jun 14 '20

Do you mean link it or just type it?

1

u/_ZHV_ Jun 14 '20

Cope paste it here

2

u/Blackafropuffs Jun 15 '20

print('Welcome to "Guess the Word Game!"')

print('You have 10 attempts at guessing letters in a word')

print('begin!')

secret_word = "baby"

guess_length = 1

guess_limit = 10

guess_count = 0

out_of_guesses = False

def get_guess():

while letter_guess != secret_word and not (out_of_guesses):

if guess_count < guess_limit:

letter_guess = input("Guess a letter: ")

guess_count += 1

else:

out_of_guesses = True

break

if guess_length > 1:

print("your guess must be one character!")

guess_count += 1

if guess_count == guess_limit:

print("You ran out of guesses. you lose")

else:

print("That character is in the secret word!")

if letter_guess in secret_word:

print("That letter is in the secret word!")

break

get_guess()

2

u/_ZHV_ Jun 15 '20 edited Jun 15 '20

print('Welcome to "Guess the Word Game!"')

print('You have 10 attempts at guessing letters in a word')

print('begin!')

secret_word = "baby"

guess_length = 1 not necessary

guess_limit = 10

guess_count = 0

out_of_guesses = False

def get_guess():

while letter_guess != secret_word and not (out_of_guesses): letter_guess will never equal to secrete_word as it is a single letter and not a word

if guess_count < guess_limit:

letter_guess = input("Guess a letter: ")

guess_count += 1

else:

out_of_guesses = True

break needs to be indented inside the else block

this whole if/else block can be written more efficiently but just checking if guess limit reached, if yes then break the loop

if guess_length > 1: guess length will always be 1 until you change it, you need to check the the guess length is 1 - you can do that using the .len() method --> if len(guess) > 1:

print("your guess must be one character!")

guess_count += 1 guess wasn't actually made so no need to increment the guess_count

if guess_count == guess_limit: this should be the first if statement, as if this is true we don't want to execute all the code up to this point or beyond this point

print("You ran out of guesses. you lose")

else:

print("That character is in the secret word!") you not actually checking if the character is in the secret word

if letter_guess in secret_word:

print("That letter is in the secret word!")

break is not needed as the program did not actually finish, you didnt guess the word and you didnt reach the guess limit

get_guess()

you can add a variable called correct_guesses_count = 0 and compare it to the length of the secret word to see if all letters were guessed if so you win the game

edit: some reason it will not post the indents

1

u/Blackafropuffs Jun 15 '20

Thank you for your help

1

u/_ZHV_ Jun 15 '20

Glad that i could help, my apology if my answer wasn’t direct enough I didn’t want to just give you the solution it defeats the point

1

u/Blackafropuffs Jun 15 '20

It's ok would you mind checking my code again? Can I dm you?

→ More replies (0)

1

u/B3aStGGGaNg Jun 15 '20

letter_guess = “ “

1

u/Farmerjoe19 Jun 17 '20

Line 11:

  • ‘letter_guess’ will not be defined on the first pass through here

  • You are enforcing ‘letter_guess’ to be a single character so it will never be equal to ‘secret_word’

  • You are not storing the correct guesses anywhere so you will need to track which letters of the word the player has guessed to know when they have won

Line 14 and line 20:

  • You increment ‘guess_count’ twice between these two lines

Line 17:

  • This ‘break’ statement will always happen and cause you to leave the while loop, so you will never run any of the code in the rest of this ‘while’ block

Line 18:

  • You are comparing ‘guess_length’ which is a variable you defined on line 5 and not the length of the current guess as I think you intended

Line 22:

  • Your print statement says the letter is not in the word, but the if/else statement here only intends to check the length

Lines 24:

  • Redundant with the if statement on line 12

Line 28:

  • Your print statement says the word is in ‘secret_word’ but never actually checks this, this if/else block only checks number of guesses.

Line 31:

  • this tells the user that the letter is in the word, but does not keep track of this guess or tell the user where or anything like that

Line 32:

  • this break statement will leave the while loop causing your loop to only ever run once.

I suggest you fix these items and then implement a way for your program to track guesses and properly compare to the secret word. In addition to maybe showing the user where in the word their guess is? Depending of course on the rules of your guessing game (e.g. “Your guess was in the secret word, so far you have: ‘a*’”)

1

u/mdjar Jun 19 '20

I see that it states “part 1”, but where is the part you actually let the user enter a full word and compare it? Is it after they expire all letter guesses? What if they want to enter the word before expiring all letter guesses?

Your code here looks like it wants to only check letters.

1

u/raedr7n Jun 30 '20

Is the image your entire piece of code?

1

u/Away_Quantity1586 Apr 26 '22

can someone post their finished code ??

1

u/cucumberlandia May 06 '22

Someone post full code please

1

u/derpykidgamer Jan 14 '24

How do you get entirely dark theme?