Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Jogo - Até a aula 3

Tomei a liberdade de seguir um pouco além das instruções dadas até o momento.

Tentei também manter escrever o jogo com o mínimo de linhas possíveis com base no meu conhecimento até o momento, mas gostaria de saber quais seriam outras formas de otimização e boas práticas para melhorar esse programa.

import random

print("Guess the number! Type a number and try finding the secret number.")

secret_num = random.randint(1,100)

guess = int(input("Type a number: "))


def guessNumber(guess):
    if guess == secret_num:
        print(f"You typed {guess} and got the correct secret number!")
        print("Congratulations!")
        return  
    elif guess > secret_num:
        print(f"You typed {guess}, but it is an incorrect answer. Try something smaller")
        guess = int(input("Type a number: "))
        guessNumber(guess)    
    elif guess < secret_num:
        print(f"You typed {guess}, but it is an incorrect answer. Try something bigger")
        guess = int(input("Type a number: "))
        guessNumber(guess)

guessNumber(guess)
1 resposta
solução!

Olá, o código está bom, acredito que algumas melhorias seriam as seguintes:

from random import randint

print("Guess the number! Type a number and try finding the secret number.")

secret_num = randint(1,100)

guess = int(input("Type a number: "))

def guessNumber(guess):
    if (guess == secret_num):
        print(f"You typed {guess} and got the correct secret number!\nCongratulations!")
        return  
    elif (guess > secret_num):
        print(f"You typed {guess}, but it is an incorrect answer. Try something smaller")
        guess = int(input("Type a number: "))
        guessNumber(guess)    
    elif (guess < secret_num):
        print(f"You typed {guess}, but it is an incorrect answer. Try something bigger")
        guess = int(input("Type a number: "))
        guessNumber(guess)

guessNumber(guess)
  • Importe apenas a função que você irá utilizar
  • Utilize parênteses ao definir condições
  • Utilize \n para quebrar linhas ao invés de dois prints Bons estudos :)

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software