(Oct-31-2022, 10:15 PM)wthiierry Wrote: Letters that are not present at all in the secret word show a "underscore _".
Letters that are present in the secret word, but in a different spot show in 'lowercase".
Letters that are present in the secret word at that exact spot, show in "uppercase".
It's always good to have a plan. So:
Letters that are present in the secret word at that exact spot, show in "uppercase".
There are many ways to achieve that but one of them is iterating over secret word and guess in parallel using built-in zip and check if characters are equal:
for guess, secret in zip(guess_word, secret_word): if guess == secret: # do somethingLetters that are present in the secret word, but in a different spot show in 'lowercase".
If we already iterate over word we should also check whether character from guess_word is in secret_word. We can check it directly or use Python special data type for membership testing - set. In order to avoid overwriting result of
if
branch we should put this secondary check into elif branch ensuring that it will run only if characters are not equal:elif guess in secret_word: # do something # or chars = set(secret_word) # should be outside of for-loop elif guess in chars: # do somethingLetters that are not present at all in the secret word show a "underscore _".
If we have handled two cases in if and elif branch and anything that passes them is character what is not in same place nor in secret_word at all. So we can direct everything else to
else
branch:else: # do somethingHint: using print function argument
end
we are able to print different loop iterations in one row:for char in 'mosiah': print(char) # will print: m o s i a h for char in 'mosiah': print(char, end='') # will print mosiahThis of course assumes that validation of letters to be in same case and words are equal length has been made.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.