Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

word guessing game python

import random
# library that we use in order to choose
# on random words from a list of words
 
name = input("What is your name? ")
# Here the user is asked to enter the name first
 
print("Good Luck ! ", name)
 
words = ['rainbow', 'computer', 'science', 'programming',
         'python', 'mathematics', 'player', 'condition',
         'reverse', 'water', 'board', 'geeks']
 
# Function will choose one random
# word from this list of words
word = random.choice(words)
 
 
print("Guess the characters")
 
guesses = ''
 
# any number of turns can be used here
turns = 12
 
 
while turns > 0:
     
    # counts the number of times a user fails
    failed = 0
     
    # all characters from the input
    # word taking one at a time.
    for char in word:
         
        # comparing that character with
        # the character in guesses
        if char in guesses:
            print(char, end=" ")
             
        else:
            print("_")
            print(char, end=" ")
             
            # for every failure 1 will be
            # incremented in failure
            failed += 1
             
 
    if failed == 0:
        # user will win the game if failure is 0
        # and 'You Win' will be given as output
        print("You Win")
         
        # this print the correct word
        print("The word is: ", word)
        break
     
    # if user has input the wrong alphabet then
    # it will ask user to enter another alphabet
    print()
    guess = input("guess a character:")
     
    # every input character will be stored in guesses
    guesses += guess
     
    # check input with the character in word
    if guess not in word:
         
        turns -= 1
         
        # if the character doesn’t match the word
        # then “Wrong” will be given as output
        print("Wrong")
         
        # this will print the number of
        # turns left for the user
        print("You have", + turns, 'more guesses')
         
         
        if turns == 0:
            print("You Loose")
Comment

PREVIOUS NEXT
Code Example
Python :: tkinter button hide 
Python :: how to declare a variable in python 
Python :: python input code 
Python :: execute terminal command from python 
Python :: how to access dataframe row by datetime index 
Python :: python super 
Python :: read excel spark 
Python :: changing the port of django port 
Python :: python script as service linux 
Python :: progress bar python 
Python :: legend font size python matplotlib 
Python :: numpy randint 
Python :: python pil 
Python :: procfile for django heroku 
Python :: python 3d array 
Python :: selenium select element by id 
Python :: python code with sigma 
Python :: one hot encoding 
Python :: python look up how many rows in dataframe 
Python :: python isset 
Python :: discord.py edit messages 
Python :: pygame point at mouse 
Python :: pandas legend placement 
Python :: python slice notation 
Python :: python append value to dictionary list 
Python :: how to add a fuction in python 
Python :: dir() in python 
Python :: how to assign a new value in a column in pandas dataframe 
Python :: python filter dict 
Python :: python datetime get weekday name 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =