Python Forum
Need some help creating a word game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help creating a word game
#5
(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 something
Letters 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 something
Letters 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 something
Hint: 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
mosiah
This 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.
Reply


Messages In This Thread
Need some help creating a word game - by wthiierry - Oct-31-2022, 10:15 PM
RE: Need some help creating a word game - by rob101 - Nov-01-2022, 01:50 AM
RE: Need some help creating a word game - by rob101 - Nov-01-2022, 11:36 AM
RE: Need some help creating a word game - by perfringo - Nov-01-2022, 12:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Scramble word game Zatoichi 9 8,611 Sep-20-2021, 03:47 PM
Last Post: jefsummers
  problem on creating a small game nayo43 5 2,900 Dec-13-2020, 01:03 PM
Last Post: jefsummers
  Code: Creating a basic python game? searching1 5 3,553 Nov-12-2018, 05:18 AM
Last Post: searching1
  Word based Game of 21 DatNerdKid 2 93,253 Aug-24-2018, 03:25 PM
Last Post: DuaneJack
  scrabble word game atux_null 4 8,905 Nov-10-2017, 10:00 AM
Last Post: atux_null

Forum Jump:

User Panel Messages

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