Is this gussing game code - good for a beginner (python)? Any suggestion for improvment?

will function make the code better?

user_name = input("Please enter your name: ")
hello = input("\nhey " +user_name + ", are you ready? " )
while hello.lower() == "no":
  print("\nso why are you here?! hehe")
  break
if hello.lower() == "yes":
  print("\nlet's start playing!")
  print('''\n\n You have 3 hints to the secret word, then you'll have to guess it.

# People like to eat it
# It's round
# It has a lot of cheese

        GOOD LUCK!
''')
  secret_word = ["pizza", "cucumber", "coffee"]
  guess = input("enter your guess ")
  while guess != secret_word[0]:
    guess = input("It's not the correct word, please try again: ")
  if guess == secret_word[0]:
    print("well done!")
  another_game = str(input("Do you want to play another game?"))
  while another_game.lower() == "no": 
    print("\nso why are you here?! hehe")
    break
  if another_game.lower() == "yes":
    print("\nlet's start playing!")
    print('''\n\n You have 3 hints to the secret word, then you'll have to guess it.

# People like to eat it for breakfast
# It's green
# It has long shape

        GOOD LUCK!
''')
    guess = input("enter your guess ")
    while guess != secret_word[1]:
      guess = input("It's not the correct word, please try again: ")
    if guess == secret_word[1]:
      print("well done!")
    another_game = str(input("Do you want to play another game?"))
    while another_game.lower() == "no": 
      print("\nso why are you here?! hehe")
      break
    if another_game.lower() == "yes":
      print("\nlet's start playing!")
      print('''\n\n You have 3 hints to the secret word, then you'll have to guess it.

# People like drinking it to feel more awake
# It's brown
# It has srong smell

        GOOD LUCK!
''')
      guess = input("enter your guess ")
      while guess != secret_word[2]:
        guess = input("It's not the correct word, please try again: ")
      if guess == secret_word[2]:
        print("well done!")
print("\nGAME OVER")

Guessing game are good for beginners. I would recommend having a look at functions as they are helpful for repeated code. Just call the function instead of re-typing the same code over and over.

A basic example, the code will continue to call the function as long as y is entered.

def _func(name):
    '''
    Function will ask to play again. If ask == y, returns True else returns False
    '''
    ask = input(f'{name}, would you like to play again? (y/n)\n>> ').lower()

    if ask == 'y':
        return True
    
    print(f'Goodbye {name}')
    return False
    
# Get user input
name = input('Enter your name: ')

# Start a while loop
while True:
    # Assign function to a variable
    question = _func(name)
    
    # If question returns true print ok, else exit loop
    if question:
        print('ok')
    else:
        break
1 Like

You may use for loops for the 3 cycles of word guessing as an alternative.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.