Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Letter Game Introduction

Roberto Núñez
Roberto Núñez
4,256 Points

My code solved the issue but... what about quality?

It seems that the code I wrote works just fine (it even solve the repeated letter issue) but after seeing the professor solution it kept me wondering if I just made a mess of code. I mean the solution was very different and didn't seem as well planned so I begin to wonder about code quality. How can I know if my code was good quality? Are there any industry rules to follow?

import random

wordlist = [aloha","banana","piña colada"]
print("\n Our supreme AI is selecting a word from the list you choose...\n...LISTO! Let's start!")
secret_word = random.choice(wordlist)
already_tried = []
game_status = []
for letter in secret_word:
    game_status.append("_")
try_num = 3
while True:

    print("\n ----------------------------------------------\n")
    display = " ".join(game_status)
    print(display)
    if try_num >=1:
        guess = input("\nYou have only {} attemps left. Shoot: ".format(try_num)).lower()
        while len(guess)!=1:
            if len(guess)>1:
                guess = input("\nHey hey hey! You cheater. Only one letter allowed \nShoot: ")
            else:
                guess = input("\nYou do know just pressing enter won't work right?. \nShoot: ")
        while guess in already_tried:
            guess = input("\nRepeating a letter won't change the result. \nShoot: ")
        already_tried.append(guess)
        if guess in secret_word:
            place = 0
            for letter in secret_word:
                if letter == guess:
                    game_status[place]=guess
                place += 1
            print("\nThat's a hit!... we do have the letter: {} ".format(guess))
        else:
            try_num -= 1
            print("\nNoup...miss.")
    else:
        print("\nNice try but noup... \nThe word was: " + secret_word)
        break

    if "_" not in game_status:

        print("\nCongrats! You completed the word. \nLa palabra era:" + secret_word)
        break

1 Answer

Matt Nickele
Matt Nickele
468 Points

On idea is rather than using break make the while loop false. I would make a variable = True and once I needed the loop to end I could make variable = False.

Additionally there is a Chapter through Treehouse that shows you how to format python could that I think would help https://teamtreehouse.com/library/write-better-python

Finally true using if elif and else statements rather then if else and then if again