Ciao a tutti, sta sera non sapevo cosa fare e ho iniziato a scrivere un gioco (così per passare il tempo, da scrivere in una mezz'oretta).
Il gioco è un impiccato, la cosa che volevo sapere è se il mio codice è abbastanza elegante o se si potrebbe migliorare
codice:
from random import randrange
print("The Hangman -- by luc99a")
running = True
tentatives = 4
#lo status GAME non serve, ma lo lascio comunque
GAME = 0
WIN = 1
GAME_OVER = 2
status = 0
def random_word():
#si le parole sono poche
words = ["hello", "car", "pizza", "ornithorhynchus", "viking"]
rand = randrange(len(words))
return words[rand]
def menu():
print("Choose an option:")
print("1) Buy letter")
print("2) Guess word")
choice = int(input(">"))
while choice != 1 and choice != 2:
print("Non valid choice, choose again")
choice = int(input(">"))
return choice
def hint(word, buyed_letters):
vowels = ["a", "e", "i", "o", "u"]
#si so che questo qui sotto è orribile da vedere
hint = [i if i in buyed_letters else "+" if i in vowels else "-" for i in word]
return hint
def check_win(word, letters, guess):
if guess == word:
return True
for i in word:
if not i in letters:
return False
return True
letters = []
word = random_word()
guess = ""
while running:
print(" ".join(hint(word, letters)))
choice = menu()
if choice == 1:
print("Wich letter would you like to buy?")
letter = input(">")
if letter in word:
letters.append(letter)
if check_win(word, letters, guess):
running = False
status = WIN
else:
tentatives -= 1
if tentatives == 0:
running = False
status = GAME_OVER
elif choice == 2:
print("Guess the world")
guess = input(">")
if check_win(word, letters, guess):
running = False
status = WIN
else:
running = False
status = GAME_OVER
if status == WIN:
print("You win")
elif status == GAME_OVER:
print("Game Over")
Grazie
Ah scusate l'ot:
chiedo scusa se rispondo raramente, ma quando sono al computer non ho tempo di rispondere, cercherò di essere più attivo in futuro
Ho notato che ho sbagliato il titolo, ero distratto e ho scritto solo [Python] scusate