ファンシーナンバー推測ゲーム


この記事は、彼らのコーディング経歴を開始したい新しいと経験のないコーダーに向けて調整されます
我々が何をコード化するかのデモ

C:\Users\Josh\Desktop>guessing.py
Enter player name: JoshTheCodingAddict

Hi JoshTheCodingAddict Welcome to my fancy number guessing game
=====================
| enter 10---Easy   |
| enter 20---Medium |
| enter 30---Hard   |
=====================

[*] Select difficulty level: 10
[*] You have 3 number of guesses left!
[!] Guess the lucky number: 9
[-] Sorry please try again

[*] You have 2 number of guesses left!
[!] Guess the lucky number: 8
[-] Sorry please try again

[*] You have 1 number of guesses left!
[!] Guess the lucky number: 7
[-] Sorry please try again

[!] Game over, type c to retry and q to quit game: c
[*] You have 3 number of guesses left!
[!] Guess the lucky number: 7
[-] Sorry please try again

[*] You have 2 number of guesses left!
[!] Guess the lucky number: 6
[-] Sorry please try again

[*] You have 1 number of guesses left!
[!] Guess the lucky number: 5
[-] Sorry please try again

[!] Game over, type c to retry and q to quit game: 4
[-] Wrong input
[!] Game over, type c to retry and q to quit game: c
[*] You have 3 number of guesses left!
[!] Guess the lucky number: 3
[-] Sorry please try again

[*] You have 2 number of guesses left!
[!] Guess the lucky number: 2
[+] Congratulations!! you won the game

[!] Thanks for playing!

ゲーム要件:

  • プレーヤー名
  • ゲームレベル:簡単、中間、ハード
  • 推測の
  • :多くの間違った推測は、ゲーム
  • を終える前に
  • 空想的な命令
  • 飛び込みましょう

    ゲームロジック

  • ユーザーは歓迎メッセージを提示し、ゲームの難易度
  • を選択するように求められます
  • それからゲーム開始とプレーヤーは、幸運なナンバー
  • を推測する機会のN数を持ちます
  • が正しいならば、プレーヤーは祝辞メッセージ
  • で示されます
  • プレーヤーが推測しない場合は、プレーヤーは、彼らは正しい番号を推測し、再試行したり、ゲームを終了する必要がありますそれらを伝えるメッセージを提示されます.
  • 更なるADOなしで、我々の手を汚くして、コードを書きましょう

    使用するモジュール

    random -擬似乱数を生成するために使用
    それで、我々はランダムを輸入することから始めます
    import random
    
    次に、私たちの変数を設定する:プレイヤー名、ウェルカムメッセージ、おめでとうメッセージ、失敗したメッセージ、難易度、LuckyCage番号と推測数
    player_name = input("Enter player name: ")
    welcome_message = f"""
    Hi {player_name} Welcome to my fancy number guessing game
    =====================
    | enter 10---Easy   |
    | enter 20---Medium |
    | enter 30---Hard   |
    =====================
    """
    print(welcome_message)
    congratulations_message = "[+] Congratulations!! you won the game\n"
    failed_message = "[-] Sorry please try again\n"
    difficulty = int(input("[*] Select difficulty level: "))
    lucky_number = random.randint(1, difficulty)
    guesses = 3
    
    今ゲームのループを起動し、推測と推測推測のロジックを書く
    while guesses >= 0:
        if guesses == 0:
            retry = input("[!] Game over, type c to retry and q to quit game: ")
            if retry == "c": guesses = 3
            elif retry == "q": break
            else: print("[-] Wrong input")
        else:  
            print(f"[*] You have {guesses} number of guesses left!")
            guessed = int(input("[!] Guess the lucky number: "))
            if guessed == lucky_number:
                print(congratulations_message)
                break
            else:
                print(failed_message)
                guesses = guesses - 1
    
    print("[!] Thanks for playing!")
    

    フルコード


    import random
    player_name = input("Enter player name: ")
    welcome_message = f"""
    Hi {player_name} Welcome to my fancy number guessing game
    =====================
    | enter 10---Easy   |
    | enter 20---Medium |
    | enter 30---Hard   |
    =====================
    """
    print(welcome_message)
    congratulations_message = "[+] Congratulations!! you won the game\n"
    failed_message = "[-] Sorry please try again\n"
    difficulty = int(input("[*] Select difficulty level: "))
    lucky_number = random.randint(1, difficulty)
    guesses = 3
    while guesses >= 0:
        if guesses == 0:
            retry = input("[!] Game over, type c to retry and q to quit game: ")
            if retry == "c": guesses = 3
            elif retry == "q": break
            else: print("[-] Wrong input")
        else:  
            print(f"[*] You have {guesses} number of guesses left!")
            guessed = int(input("[!] Guess the lucky number: "))
            if guessed == lucky_number:
                print(congratulations_message)
                break
            else:
                print(failed_message)
                guesses = guesses - 1
    
    print("[!] Thanks for playing!")
    
    
    これとハッピーコーディングを改善してください.