Accede a todo EDteam con un único pago¡Sube a premium!
Banner de perfil
0

David A. Rodas

@davida-rodas

Asunción, Paraguay

Mi código en Python

David A. Rodas@davida-rodas

Comparto mi código en Python, utilizando los conocimientos y recomendaciones compartidas hasta este punto en la clase.

Agradecería sugerencias para mejorar el código y/o lógica.

1import random 2 3total_attempts = 3 4min_number = 1 5max_number = 20 6games_played = 0 7won_games = 0 8 9isOver = False 10isBckdrActived = False 11 12txt_game_title = "Game \"Guess the Hidden Number\"." 13txt_attempt_left = "{} more attempt(s)" 14txt_first_input = "-> Guess the hidden number between {} and {}: " 15txt_input_with_hint = "-> Guess the hidden number between {} and {} (Hint: {} and {}): " 16txt_tip = "You should to input a {} number." 17txt_discover = "[The hidden number is {}]" 18txt_bckdr = "__HIDDEN [{}] NUMBER__" 19txt_u_win = "YOU GUESSED IT!" 20txt_u_lose = "You lose :(\nGood Luck next time." 21txt_invalid_warning = "Invalid number! Try again..." 22txt_play_again = "Play again? (Y/N): " 23txt_game_over = ":::: GAME OVER ::::" 24txt_games_played = "Games played: {}" 25txt_won_lose_games = "{} games: {}" 26txt_player_efficent = "Efficent: {}%" 27txt_middle_dash = "------------------------------" 28txt_asteriks = "****************************" 29txt_middle_under_dash = "-_-_-_-_-_-_-_-_-_-_-_-_-_-_" 30 31while ~isOver: 32 games_played = games_played + 1 33 attempts_remain = total_attempts 34 smaller_number = min_number 35 greater_number = max_number 36 isAvailableToPlay = attempts_remain > 0 37 hidden_number = random.randint(min_number, max_number) 38 print(txt_game_title) 39 40 while isAvailableToPlay: 41 isBeginning = attempts_remain == total_attempts 42 print(txt_attempt_left.format(attempts_remain)) 43 isGameOver = attempts_remain == 0 44 45 if isBckdrActived: 46 print(txt_bckdr.format(hidden_number)) 47 48 if isBeginning: 49 print(txt_first_input.format(min_number, max_number)) 50 elif isGameOver: 51 isOver = True 52 print(txt_middle_under_dash) 53 print(txt_u_lose) 54 print(txt_discover.format(hidden_number)) 55 print(txt_middle_under_dash) 56 break 57 else: 58 print(txt_input_with_hint.format(min_number, max_number, smaller_number, greater_number)) 59 60 try_to_guess = int(input()) 61 62 isValidNumber = try_to_guess >= min_number and try_to_guess <= max_number 63 isSmallerThanHiddenNumber = try_to_guess < hidden_number 64 isGreaterThanHiddenNumber = try_to_guess > hidden_number 65 66 if isValidNumber: 67 if isGreaterThanHiddenNumber: 68 attempts_remain = attempts_remain - 1 69 greater_number = try_to_guess 70 print(txt_tip.format("least")) 71 print(txt_middle_dash) 72 elif isSmallerThanHiddenNumber: 73 attempts_remain = attempts_remain - 1 74 smaller_number = try_to_guess 75 print(txt_tip.format("greatest")) 76 print(txt_middle_dash) 77 else: 78 print(txt_asteriks) 79 print(txt_u_win) 80 print(txt_discover.format(try_to_guess)) 81 print(txt_asteriks) 82 isOver = True 83 won_games = won_games + 1 84 break 85 else: 86 print(txt_invalid_warning) 87 print(txt_middle_dash) 88 89 if isOver: 90 answer = input(txt_play_again) 91 willContinue = answer == 'y' or answer == 'Y' 92 if willContinue: 93 isOver = False 94 else: 95 print(txt_game_over) 96 break 97 98lose_games = games_played - won_games 99player_efficent = (games_played * 100) / won_games 100print(txt_middle_under_dash) 101print(txt_games_played.format(games_played)) 102print(txt_won_lose_games.format("Won", won_games)) 103print(txt_won_lose_games.format("Lose", lose_games)) 104print(txt_player_efficent.format(player_efficent)) 105print(txt_middle_under_dash)

Mi versión de Número Oculto

David A. Rodas@davida-rodas

Esta es mi versión del juego en Python, aplicando los conocimientos y recomendaciones que compartieron en este curso.

Espero sugerencias para mejorar el código y/o lógica.

1import random 2 3total_attempts = 3 4min_number = 1 5max_number = 30 6 7isOver = False 8isBackdoorActived = False 9 10txt_game_title = "Game \"Guess the Hidden Number\"." 11txt_attempt_left = "{} more attempt(s)" 12txt_first_input = "-> Guess the hidden number between {} and {}: " 13txt_input_with_tip = "-> Guess the hidden number between {} and {} (Tip: {} and {}): " 14txt_tip = "You should to input a {} number." 15txt_discover = "[The hidden number is {}]" 16txt_backdoor = "__HIDDEN [{}] NUMBER__" 17txt_u_win = "YOU GUESSED IT!" 18txt_u_lose = "You lose :(\nGood Luck next time." 19txt_invalid_warning = "Invalid number! Try again..." 20txt_play_again = "Play again? (Y/N): " 21txt_game_over = ":::: Game Over ::::" 22txt_middle_dash = "------------------------------" 23txt_asteriks = "****************************" 24txt_middle_under_dash = "-_-_-_-_-_-_-_-_-_-_-_-_-_-_" 25 26while ~isOver: 27 attempts_remain = total_attempts 28 least_number = min_number 29 greater_number = max_number 30 isAvailableToPlay = attempts_remain > 0 31 hidden_number = random.randint(min_number, max_number) 32 print(txt_game_title) 33 34 while isAvailableToPlay: 35 isBeginning = attempts_remain == total_attempts 36 print(txt_attempt_left.format(attempts_remain)) 37 isGameOver = attempts_remain == 0 38 39 if isBackdoorActived: 40 print(txt_backdoor.format(hidden_number)) 41 42 if isBeginning: 43 print(txt_first_input.format(min_number, max_number)) 44 elif isGameOver: 45 isOver = True 46 print(txt_middle_under_dash) 47 print(txt_u_lose) 48 print(txt_discover.format(hidden_number)) 49 print(txt_middle_under_dash) 50 break 51 else: 52 print(txt_input_with_tip.format(min_number, max_number, least_number, greater_number)) 53 54 try_to_guess = int(input()) 55 56 isValidNumber = try_to_guess >= min_number and try_to_guess <= max_number 57 isLeastThanHiddenNumber = try_to_guess < hidden_number 58 isGreaterThanHiddenNumber = try_to_guess > hidden_number 59 60 if isValidNumber: 61 if isGreaterThanHiddenNumber: 62 attempts_remain = attempts_remain - 1 63 greater_number = try_to_guess 64 print(txt_tip.format("least")) 65 print(txt_middle_dash) 66 elif isLeastThanHiddenNumber: 67 attempts_remain = attempts_remain - 1 68 least_number = try_to_guess 69 print(txt_tip.format("greatest")) 70 print(txt_middle_dash) 71 else: 72 print(txt_asteriks) 73 print(txt_u_win) 74 print(txt_discover.format(try_to_guess)) 75 print(txt_asteriks) 76 isOver = True 77 break 78 else: 79 print(txt_invalid_warning) 80 print(txt_middle_dash) 81 82 if isOver: 83 answer = input(txt_play_again) 84 willContinue = answer == 'y' or answer == 'Y' 85 if willContinue: 86 isOver = False 87 else: 88 print(txt_game_over) 89 break

Pregunta a ChatEDT