dimanche 1 janvier 2023

How to improve my code in this SIMPLE game program [closed]

I am new to programming and this forum, i am on my second day of learning and i have written the following code. My question is, how would you optimize this? It looks quite messy, right now i am trying to learn how to code in genereal so function is my priority but i would like to know who other experienced programmors would rewrite the following;

import random 
number = random.randint(0,10)
guess = 5

name = input("what is your name? ")
user_guess = input(f"Hello {name}, guess a number between 0-10.")


while guess != 0:
  if int(user_guess) != number:
    guess -= 1
    user_guess = input(f"You are wrong, guess again. You have {guess} guesses remaining")
  elif int(user_guess) == number:
    print(f"You are indeed correct {name}, the number was {number}")
    break
if guess == 0:
    print(f"You are wrong, the number was {number}")


Its working but it looks very messy.




Aucun commentaire:

Enregistrer un commentaire