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!
So - ChatGPT is great for breaking down concepts like this and answering questions about the basics until you get the hang of it. I would recommend crafting some programs while asking it for help on anything you get stuck with, until you can craft the programs without involving it (and still you can paste the programs into it and ask for pointers / fixes that it can see). I’m currently learning Go with assistance from ChatGPT and it’s hugely useful.
One other angle you could come at it from – this might venture into unpopularopinion territory, but I would actually recommend learning C at a very early stage. It’ll be tedious to make nontrivial programs with it, so you may not want to stick with it for your real projects, but since everything is bytes and pointers it gives you a chance to get extremely solid with the fundamentals before you start mucking around with slices / hashes / etc. I would recommend to try to get this particular problem working using C, which will be more work, but then when you come back and learn the Python concepts I think they’ll make a lot of sense and you’ll feel capable with them. IDK if it’ll work that way for you, but that’s what I did and I feel like it was a good way to go.
Best of luck + keep plugging
Yeah I’ve actually been using chatgpt as well as a few other resources! My biggest gripe is that chatgpt can’t really teach without showing. I want to understand where my logic was flawed, and be guided towards the correct answer, instead chatgpt will do a good job at explaining what I did wrong, and then showing me the correct code.
So c is a good starter language? Cuz I’m at the point now that I can just stackoverflow my way into making a smaller project, but I really want to learn how this all works and learn the fundamentals so I’m fluent