31
submitted 1 year ago* (last edited 1 year ago) by Osnapitsjoey@lemmy.one to c/programming@programming.dev

so ill post a few of my failed examples below along with what I came up with as a fix, and then the actual correct code. I feel like im so close to grasping this, but missing some logic. this is for a hangman game.

one of the failed attempts:

import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)

#Testing code
print(f'Pssst, the solution is {chosen_word}.')

#Create an empty List called display.
#For each letter in the chosen_word, add a "_" to 'display'.
#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.


display = ["_"] * len(chosen_word)


guess = input("Guess a letter: ").lower()

#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].

for letter in chosen_word:
if guess == letter:
for i in range(len(chosen_word)):
display.insert(i, guess)

print(display)

second:

for letter in chosen_word:
  if guess == letter:
    for i in range(len(chosen_word[letter])):
      display.insert(i, guess)

I ended up just saying screw it and went to this:

display = []
for char in chosen_word:
    if guess == letter:
        display += letter
   else:
    display += "_"

correct way of doing it:

import random
word_list = ["aardvark", "baboon", "camel"]
chosen_word = random.choice(word_list)

print(f'Pssst, the solution is {chosen_word}.')

display = []
word_length = len(chosen_word)
for _ in range(word_length):
  display += "_"
print(display)
  
guess = input("Guess a letter: ").lower()


for position in range(word_length):
  letter = chosen_word[position]
  if letter == guess:
    display[position] = letter

print(display)

so as you can see, i get that I can grab specific parts of a list using indices or slices, but somewhere in my brain my logic is wrong. if you guys have struggled with this before or if you have a good youtube video to help me break it down id be beyond thankful!

you are viewing a single comment's thread
view the rest of the comments
[-] WillRegex@lemm.ee 6 points 1 year ago

I think you're misunderstanding the insert method. insert keeps everything that was already in the list, including the item at the given index. it just shifts part of it to the right to make room for the thing you're inserting. Directly changing the list using display[position] = letter instead replaces the item at the index with letter.

Does that make sense?

[-] Osnapitsjoey@lemmy.one 1 points 1 year ago* (last edited 1 year ago)

Yeah I figured that one out from the documentation. I with I saved more of my trial and errors so I could show you guys what I needed help with better. I tried the list.insert(x) and then I would do a list.pop(i+1) as well ๐Ÿ˜‚

I guess what I need help with is I keep messing up where I would put "for x in y: z= y [in position z

I had a few tries with it written

For letter in range(len(chosen_word)) : If letter == guess: Display[I] = letter

But this would grab all the letters and change all blanks to the guess letter

[-] potterman28wxcv@beehaw.org 3 points 1 year ago

Instead of blindly trying code until it works I would suggest you to write on paper the distinct steps that are required to solve the problem.

Imagine you are the computer and you can do nothing else but what Python allows you. How do you solve the problem ?

Usually people do this exercise on a small example. Then they generalise the approach when they find examples where it does not work.

[-] Osnapitsjoey@lemmy.one 2 points 1 year ago

Ahhh you know what. This would help me. Because when I'm stumped, I'm definitely just "blindly" trying different orders of things and getting frustrated. Thank you very much for the tip

[-] potterman28wxcv@beehaw.org 1 points 1 year ago

Yeah sometimes you just have to take a step back and think again. Then you will think more clearly and actually know what you wrote :) good luck!

load more comments (2 replies)
this post was submitted on 07 Sep 2023
31 points (89.7% liked)

Programming

17314 readers
231 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS