Spaces:
Running
Running
Download classroom_data/templates/week23/main.py from Telearn/Project_Demo: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/Telearn/Project_Demo/resolve/main/classroom_data/templates/week23/main.py
- Command line
-
hf download hf://spaces/Telearn/Project_Demo/classroom_data/templates/week23/main.py
-
curl -L -o main.py https://huggingface.co/spaces/Telearn/Project_Demo/resolve/main/classroom_data/templates/week23/main.py
1.89 kB
| """荒謬商品拍賣會:使用虛擬幣,所有資料只留在本次執行。""" | |
| PRODUCT = "永遠慢一天的作業提醒手錶" | |
| BUDGET = 100 | |
| START = 10 | |
| STEP = 5 | |
| PLAYERS = ["甲", "乙", "丙"] | |
| def ask_bid(name, minimum): | |
| if minimum > BUDGET: | |
| return 0 | |
| while True: | |
| raw = input(f"{name},出價 {minimum}~{BUDGET} 幣,0 放棄:").strip() | |
| try: | |
| bid = int(raw) | |
| except ValueError: | |
| print("請輸入整數。") | |
| continue | |
| if bid == 0 or minimum <= bid <= BUDGET: | |
| return bid | |
| print(f"出價需在 {minimum}~{BUDGET} 幣之間。") | |
| def open_round(players): | |
| """每次新出價都公開;全員一整輪沒有加價即開標。""" | |
| winner, price = "", 0 | |
| while True: | |
| changed = False | |
| for name in players: | |
| if name == winner: | |
| continue | |
| minimum = price + STEP if winner else START | |
| bid = ask_bid(name, minimum) | |
| if bid: | |
| winner, price, changed = name, bid, True | |
| print(f"目前最高:{winner}|{price} 幣") | |
| if not changed or price >= BUDGET: | |
| return winner, price | |
| def announce(result): | |
| winner, price = result | |
| if not winner: | |
| print("流標:沒有符合條件的出價。") | |
| return | |
| print(f"{winner} 得標|成交 {price} 幣|剩餘 {BUDGET - price} 幣") | |
| def main(): | |
| if not 1 <= START <= BUDGET or STEP < 1: | |
| print("請設定 1 ≤ 起標價 ≤ 預算,最低加價至少 1 幣。") | |
| return | |
| print(f"\n荒謬商品拍賣會|{PRODUCT}") | |
| print(f"每人 {BUDGET} 枚虛擬幣|起標 {START}|公開加價至少 {STEP} 幣") | |
| print("輸入 0 代表這次不加價;一整輪沒人加價即開標。") | |
| announce(open_round(PLAYERS)) | |
| if __name__ == "__main__": | |
| main() | |