mo_ztt,
@mo_ztt@lemmy.world avatar

In my opinion, C is a difficult starter language, but one that sets you up for very good abilities in the future, because a lot of other languages’ core concepts are built on C concepts. It’s all bytes, and if you’re comfortable with bytes then that’ll put you ahead of someone who only knows the general concepts. It’s similar to working construction before you go into architecture – your buildings will be better even if you’re thinking at a higher level than nails and shingles when you do most of your design.

So, ChatGPT is tuned to sort of do things for you, but you can still have it do the troubleshooting with the right prompting instead of just rewriting:

GPT-4 Prompt

I’m trying to understand what’s going wrong with my code – can you point me to the line that’s causing the issue and quickly explain what might be wrong?


<span style="color:#323232;">import random
</span><span style="color:#323232;">word_list = ["aardvark", "baboon", "camel"]
</span><span style="color:#323232;">chosen_word = random.choice(word_list)
</span><span style="color:#323232;">
</span><span style="color:#323232;">#Testing code
</span><span style="color:#323232;">print(f'Pssst, the solution is {chosen_word}.')
</span><span style="color:#323232;">
</span><span style="color:#323232;">#Create an empty List called display.
</span><span style="color:#323232;">#For each letter in the chosen_word, add a "_" to 'display'.
</span><span style="color:#323232;">#So if the chosen_word was "apple", display should be ["_", "_", "_", "_", "_"] with 5 "_" representing each letter to guess.
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">display = ["_"] * len(chosen_word)
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">guess = input("Guess a letter: ").lower()
</span><span style="color:#323232;">
</span><span style="color:#323232;">#If the letter at that position matches 'guess' then reveal that letter in the display at that position.
</span><span style="color:#323232;">#e.g. If the user guessed "p" and the chosen word was "apple", then display should be ["_", "p", "p", "_", "_"].
</span><span style="color:#323232;">
</span><span style="color:#323232;">for letter in chosen_word:
</span><span style="color:#323232;">    if guess == letter:
</span><span style="color:#323232;">        for i in range(len(chosen_word)):
</span><span style="color:#323232;">            display.insert(i, guess)
</span><span style="color:#323232;">
</span><span style="color:#323232;">print(display)
</span>

GPT-4’s response

The problem with your code is in this segment:


<span style="color:#323232;">for letter in chosen_word:
</span><span style="color:#323232;">    if guess == letter:
</span><span style="color:#323232;">        for i in range(len(chosen_word)):
</span><span style="color:#323232;">            display.insert(i, guess)
</span>

The logic you’re using here is:

  • For each letter in chosen_word,
  • If guess matches the letter,
  • Insert the guessed letter at every index of the chosen_word.

This will make the display list longer than it should be, and the letters will not replace the underscores but instead will be added alongside them.

Instead, you should check if the guessed letter matches a letter in the chosen word at a specific position, and if it does, replace the underscore at that position in the display list.

Here’s the corrected segment of code:


<span style="color:#323232;">for i in range(len(chosen_word)):
</span><span style="color:#323232;">    if chosen_word[i] == guess:
</span><span style="color:#323232;">        display[i] = guess
</span>

With this, the guessed letter will replace the corresponding underscore in the display list.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines