0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python boot camp by Dr.Angela day14

Last updated at Posted at 2021-05-15

#Mission>> Higher Lower Program
AとBどちらの方がinstagramのfollower数が多いか、勝ち抜き戦をするゲーム。
正解した場合→ どんどん次の挑戦者が現れ、当てていく。
不正解の場合→ その時点でGAME OVERで、Final scoreが表示される。

余談...今までreplit(クラウド型IDEを使用していたが、connectionが途切れることが多いので今回からVisual Studioに変えた。サクサク快適である!)

higher_lower.py
import random
from game_data import data
from art import logo
import os                     #os.system('○○○')で○○○のコマンドを動作させられる
                              #今回はコンソール消すコマンドで使用
                              #Linuxならclear / Windowsならcls
def select_account():
    """新たにaccountを選定し、返す。さらにそれをリストから削除しておく"""
    NEW_CHAL = random.choice(data)
    remove2 = data.index(NEW_CHAL)
    del data[remove2]
    return NEW_CHAL

def check_answer(guess,a_follower,b_follower):
    """guess vs correctを比較してTrue or Falseを返す"""
    substraction = a_follower - b_follower
    if substraction > 0:
      return guess == "A"     #->"A","B"を返すのではなく、guessの結果とA,Bが合致していればTrueを返す
    else:
      return guess == "B"

def play():
    score = 0
    game_should_continue = True
    a = select_account()      #初回CHAMPION選定
    b = select_account()      #初回CHALLENGER選定
    
    while game_should_continue:       #gameが続く限り繰り返しaccountをget
        print(logo)
        print("[A] CHAMPION: "+ a['name']+ ", " + a['description']+", from " + a['country'])
        print("[B] CHALLENGER: "+ b['name']+ ", " + b['description']+", from " + b['country'])
        
        guess = input("\nWho has more followers? [A/B] ")
        if guess == "A":            #Aを選択した場合
            a = a                     #aはそのままaに据え置き、
            b = select_account()      #bは新たに選定
        else:                       #Bを選択した場合
            a = b                     #bはaとして置き換え、
            b = select_account()      #bを新たに選定

        a_follower = a["follower_count"]
        b_follower = b["follower_count"]
        is_correct = check_answer(guess, a_follower, b_follower)
        #check_answer()にaとbのfollower数投げて、is_correctとしてTrueかFalseをもらう
        line = "---------------------"    
        if is_correct:   #正解である限り繰り返す
            score += 1
            os.system('cls')
            print(f"\n{line}\nYou're right!\nCurrent score: [ {score} ]\n{line}\n")
        else:            #不正解ならば
            game_should_continue = False    #gameは続けない
            os.system('cls')
            print(f"\n{line}\nSorry, that's wrong.\nFinal score: [ {score} ]\n{line}\n")

play()

<今回学んだこと>
・def check_answer()の中の、return文の書き方。これでTrue/Falseが返ってくる。
・import os & os.system('任意のcommand')でOSのコマンドが実行できるようになる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?