Python Forum
Can someone help me please understand this code?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can someone help me please understand this code?
#1
I believe i have highlighted the sections i'm referring to below.


So i just want to understand the parts i'm going to ask about, because i got confused and don't want to move onto the next section until i understand them.

line 18 - 22,

i understand that name and system, the net is for windows and clr is to clear the screen and the else is there in case it is mac or something else, so either way it clears the screen, so is that the main reason for this function, that is clears my screen after typing information and then whatever need to be retained stays on the appropriate section of the screen?


line 45,
the function there that has (good, bad) for arguments, what does good and bad do, i'm confused with how arguments work honestly. So here couldn't that part of code have been written without them or did it need them to write the code?

then line 58,
what does return guess do ? Couldn't we have done print guess? I'm still trying to understand how return works as well.

then line 71,

what in the world is found = True for? I'm very confused here, as well as line 74 where found is now false? Then line 75 where, if found: what does this mean? that whole part of code from line 71 to 75 has me lost.

then line 83
where done = true and that is the argument to the play function? What does that do? then line 89 where return makes done = false? Return runs the play function and makes the argument false??? how did we run the function through return that also confuses me?

then line 99
what does return true do there? Why is it there?

then lastly what does 103
done = False do, how did done go from true to false and what did it do each time?

I really appreciate any help here, I understand most the code but i feel this will really help me get clarity.

THANK YOU ALL FOR THE HELP SO MUCH THANK YOU!!!

import random
import os
import sys

words = [
    'apple',
    'banana',
    'orange',
    'coconut',
    'strawberry',
    'lime',
    'lemon',
    'blueberry',
    'melon',
]

def clear():
    if os.name == 'net':
        os.system('cls')
    else:
        os.system('clear')[ / color]

def draw(bad, good, secret_word):
    clear()
    print('strikes: {}/7'.format(len(bad)))
    print('')
    for letter in bad:
        print(letter, end=' ')
    print('\n\n')
    for letter in secret_word:
        if letter in good:
            print(letter, end='')
        else:
            print('_', end='')
    print('')

def get_guess(bad, good):
    while True:
        guess = input("guess a letter: ").lower()
        if len(guess) != 1:
            print("you can only guess a single letter")
            continue
        elif guess in bad or guess in good:
            print("you've already guessed that letter")
            continue
        elif not guess.isalpha():
            print('you can only guess letters')
            continue
        else:
           return guess

def play(done):
    clear()
    secret_word = random.choice(words)
    bad = []
    good = []
    while True:
        draw(bad, good, secret_word)
        guess = get_guess(bad, good)
        if guess in secret_word:
            good.append(guess)
            found = True
            for letter in secret_word:
                if letter not in good:
                    found = False
            if found:
                print('win')
                print("word was {} ".format(secret_word))
        else:
            bad.append(guess)
            if len(bad) == 7:
                draw(bad, good, secret_word)
                print('you lost secret word was{} '.format(secret_word))
                done = True[ / color]
        if done:
            play_again = input('play again y/n')
            if play_again != 'n':
            else:
                sys.exit()

def welcome():
    start = input("press enter to start or q to quit ")
    if start == 'q':
        print('bye')
        sys.exit()
    else:
        return True[ / u][/color]

print('welcome to letter guess')

while True:
    clear()
    welcome()
    play(done)
Reply


Messages In This Thread
Can someone help me please understand this code? - by Jayboii478 - Apr-04-2018, 04:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 443 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Unable to understand how given code takes a fixed input value, without inputting. jahuja73 4 2,864 Jan-28-2021, 05:22 PM
Last Post: snippsat
  Don't understand example code from a textbook lil_fentanyl 1 1,911 Jan-25-2021, 07:02 PM
Last Post: nilamo
  Unable to understand a statement in an existing code ateestructural 1 2,334 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  Trying to understand the python code spalisetty 2 2,019 Mar-16-2020, 08:11 AM
Last Post: javiertzr01
  I couldn't understand the output of the below code ravich129 1 2,003 Dec-12-2019, 06:24 AM
Last Post: sandeep_ganga
  can you understand why this code prints None? arcbal 2 2,873 Mar-13-2019, 02:57 AM
Last Post: arcbal
  Don't understand why this quicksort code works lupoalberto 6 4,224 Mar-27-2018, 10:01 AM
Last Post: lupoalberto
  Can't understand the code SelectiveDuplicate 2 3,057 Mar-23-2018, 07:57 AM
Last Post: SelectiveDuplicate
  code to understand the script execution Skaperen 2 3,453 Jan-06-2018, 05:25 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020