Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word Game
#4
And one more

#! /usr/bin/env python3
import random
import string
from subprocess import call
import os

# Start score at zero
score = 0

# Create a empty list
myword = []

# Function to clear screen of cluttered text
def clear():
    _ = call('clear' if os.name == 'posix' else 'cls')

clear()

# Start the while loop
while True:

    # Get 10 letters from the alphabet
    letters = random.sample(string.ascii_lowercase, 10)

    # Print letters to choose from
    print(f'\n{letters}')
    try:
        print('\nType q and press enter to exit.\n')
        # Ask for a word
        word = input('Enter a word: ').lower()

         # A way to exit the game
        if word == 'q':
            break

        # Compare our word with the letters list and creat a list of matched letters
        [myword.append(ch) for ch in word if ch in letters]

        # Join the letters in list to make word
        myword = ''.join(myword)

        # Check to see if myword has letters and there is at least 3
        if myword and len(myword) >= 3:

            # Increase score by 1
            score += 1

            # Clear screen and print message
            clear()
            print(f'\nYour word is {myword}.')
            print(f'Boom! Your score is now {score}.')

            # Reset myword to empty list
            myword = []
        else:

            # Clear screen and print message and continue
            clear()
            print(f'\nSorry, please try again.')

            # Reset myword to empty list and continue
            myword = []
            continue
        
    except ValueError as error:
        print(error)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Word Game - by paulmerton4pope - Jul-10-2020, 12:44 PM
RE: Word Game - by GOTO10 - Jul-10-2020, 04:51 PM
RE: Word Game - by menator01 - Jul-10-2020, 04:58 PM
RE: Word Game - by menator01 - Jul-10-2020, 07:10 PM
RE: Word Game - by paulmerton4pope - Jul-11-2020, 02:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 2,966 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Python Speech recognition, word by word AceScottie 6 15,866 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  print a word after specific word search evilcode1 8 4,725 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  difference between word: and word[:] in for loop zowhair 2 3,628 Mar-03-2018, 07:24 AM
Last Post: zowhair

Forum Jump:

User Panel Messages

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