見出し画像

Python Pro Bootcamp、9日目 - 自動落札プログラムを実装する

オークションの過程をシュミレーションし、最高価格の落札者の名前や提示価格を表示するプログラムの作成を目指します。
辞書型の有効な扱い方も併せて学習しました。

辞書型の有効な扱い方

student_scores = {
  "Harry": 81,
  "Ron": 78,
  "Hermione": 99, 
  "Draco": 74,
  "Neville": 62,
}

#生徒たちの点数を評価する。
student_grades = {}

#TODO-2: Write your code below to add the grades to student_grades.👇
for student in student_scores:
  score = student_scores[student]
  if score > 90:
    student_grades[student] = "A"
  elif (90 >= score > 80):
    student_grades[student] = "B"
  elif (80 >= score):
    student_grades[student] = "C"

# 🚨 Don't change the code below 👇
Print(student_grades)

自動落札プログラム

from replit import clear
from art import logo
print(logo)

bids = {}
bidding_finished = False

#落札者の名前と落札価格を紐づけたディクショナリー型を使う
#誰が最高価格で落札したかをチェック
def find_highest_bidder(bidding_record):
  highest_bid = 0
  winner = ""
  
# bidding_record = {"Angela": 123, "James": 321}
#新しい落札者が最高価格であったら落札者に設定する
  for bidder in bidding_record:
    bid_amount = bidding_record[bidder]
    if bid_amount > highest_bid: 
      highest_bid = bid_amount
      winner = bidder
  print(f"The winner is {winner} with a bid of ${highest_bid}")

#落札が確定していない間はオークションを継続
while not bidding_finished:
  #新しい落札者・落札価格を登録
  name = input("What is your name?: ")
  price = int(input("What is your bid?: $"))
  bids[name] = price

  #落札が終了しているか否かを判定  
  should_continue = input("Are there any other bidders? Type 'yes or 'no'.\n")

  #落札者の名前・価格を表示
  if should_continue == "no":
    bidding_finished = True
    find_highest_bidder(bids)
  elif should_continue == "yes":
    clear()

次回は関数を使って入出力を行うケースを学習します。楽しみにしています。

この記事が気に入ったらサポートをしてみませんか?