Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
a guessing game where a player must guess a letter that is randomly generated by the computer. The player has unlimited guesses, and every time the player makes a guess, the computer will provide feedback about whether the guess was too high, too low or correct.

Any suggestions are welcome

What I have tried:

Python
# Guessing game 
# player guesses a random letter generated
# unlimited guesses

import random, string

def main():
    continue_game = True
    while continue_game== True:
        play_one_round()
        continue_game = play_again()
        
def play_one_round():
    # play one round of the game
    display_instructions()
    print('I am thinking of a letter between a and z.')
    
    # generate a random letter from a to z
    letter = random.choice(string.ascii_letters)
    letter = letter.lower()
    
    guess_num = 0   
    guess_list = []
    overall_guess_list=[]
    while True:
        guess = input('Take a guess: ')
        guess_list.append(guess)
        guess_num = guess_num+1
        if guess>= 'a' and guess<= 'z':
            if guess < letter:
                print('Your guess is too low.')
                continue
            elif guess > letter:
                print('Your guess is too high.')
                continue 
            elif guess== letter:
                print('Good Job, you guessed the correct letter!')
                answer = guess
                break
        else:
            print('Invalid Input') 
            continue
        
    print('---MY STATS---')
    print('Number of Guesses: ', guess_num)
    overall_guess_list.append(guess_num)       # total guesses of all games
    ranking_label(guess_num)
    worst_letter = worst_guess(guess,answer, guess_list)
    print('Worst Letter Guess: ', worst_letter)
    play_again()    
    
    
def display_instructions():
    # display  instructions
    instructions_file = open('instructions.txt', 'r')
    instructions = instructions_file.read()
    instructions_file.close()
    print(instructions)
    
def ranking_label(guess_num):
    if guess_num <5:
        print('Level: Expert')
    elif guess_num>=5 and guess_num<=10:
        print('Level: Intermediate')
    else:
        print('Level: Beginner')
        
def worst_guess(guess, answer, guess_list):
    diff_list = []
    for guess in guess_list:
        diff = abs(ord(guess)-ord(answer))
        diff_list.append(diff)
    worst_num = int(max(diff_list))
    worst_letter = abs(ord(answer)- worst_num)
    
    return chr(worst_letter)

def play_again():
    # prompt to play again
    # depends on player's choice
    again = input('Would you like to play again? Y/N ')
    if again.lower() == 'y':
        continue_game = True
    else:
        continue_game =  False
        print(summary_stat(overall_guess_list))
    return continue_game
    
def summary_stat(overall_guess_list):
    print('---SUMMARY STATS---')
    lowest = min(overall_guess_list)
    highest = max(overall_guess_list)
    print('Lowest number of Guesses: ', lowest)
    print('Highest number of guesses: ', highest)
    avg_guess = sum(overall_guess_list)/ len(overall_guess_list)
    print('Average Number of Guesses: ', avg_guess)
    if avg_guess <5:
        print('Overall Level: Expert')
    elif avg_guess>=5 and avg_guess<=10:
        print('Overall Level: Intermediate')
    else:
        print('Overall Level: Beginner')    

        
main()
Posted
Updated 6-Oct-20 20:15pm
v2
Comments
Richard MacCutchan 7-Oct-20 4:19am    
I suggest you ask a question.

Quote:
I have written the code of my question, but there are still some bugs

You forgot to explain/describe what are the bugs. When you ask for help, it is a good idea to tell what is your problem.
Quote:
I can't figure out how to solve them.

Finding all bugs just by reading code is reserved to experts because of their experience and practice.
For beginners, there is a tool, the debugger, and secondary effect, abusing it improve your learning curve.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
CPallini 7-Oct-20 2:19am    
5.
Patrice T 7-Oct-20 2:33am    
Thank you
Before debugging your code learn to do the basic
Eye through debugging. Where you look for
the basic errors with your eyes.

Errors like, indentation, semicolons
spelling and pre and post conditions of your
Functions or classes.

This becomes a useful tool in becoming an expert.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900