iT邦幫忙

2024 iThome 鐵人賽

DAY 17
0
Python

30天零基礎學習Python程式語言系列 第 17

Day 17: 骰子程式 + 函式 function

  • 分享至 

  • xImage
  •  

規則:骰出三個骰子後顯示總額和圖案
step1.印出指定骰子

import random

dice_art = {
    1: (
        "┌───────┐",
        "│       │",
        "│   ●   │",
        "│       │",
        "└───────┘",
    ),
    2: (
        "┌───────┐",
        "│ ●     │",
        "│       │",
        "│     ● │",
        "└───────┘",
    ),
    3: (
        "┌───────┐",
        "│ ●     │",
        "│   ●   │",
        "│     ● │",
        "└───────┘",
    ),
    4: (
        "┌───────┐",
        "│ ●   ● │",
        "│       │",
        "│ ●   ● │",
        "└───────┘",
    ),
    5: (
        "┌───────┐",
        "│ ●   ● │",
        "│   ●   │",
        "│ ●   ● │",
        "└───────┘",
    ),
    6: (
        "┌───────┐", # 0
        "│ ●   ● │", # 1
        "│ ●   ● │", # 2
        "│ ●   ● │", # 3
        "└───────┘", # 4
    ),
}
for i in range(5):
    print(dice_art.get(6)[i])


┌───────┐
│ ●   ● │
│ ●   ● │
│ ●   ● │
└───────┘

step2.輸入骰幾次骰子並顯示隨機數字

import random

dice_art = {
    1: (
        "┌───────┐",
        "│       │",
        "│   ●   │",
        "│       │",
        "└───────┘",
    ),
    2: (
        "┌───────┐",
        "│ ●     │",
        "│       │",
        "│     ● │",
        "└───────┘",
    ),
    3: (
        "┌───────┐",
        "│ ●     │",
        "│   ●   │",
        "│     ● │",
        "└───────┘",
    ),
    4: (
        "┌───────┐",
        "│ ●   ● │",
        "│       │",
        "│ ●   ● │",
        "└───────┘",
    ),
    5: (
        "┌───────┐",
        "│ ●   ● │",
        "│   ●   │",
        "│ ●   ● │",
        "└───────┘",
    ),
    6: (
        "┌───────┐", # 0
        "│ ●   ● │", # 1
        "│ ●   ● │", # 2
        "│ ●   ● │", # 3
        "└───────┘", # 4
    ),
}
num_dice = int(input("請輸入要擲幾個骰子:"))
dice = []
for i in range(num_dice):
    dice_number = random.randint(1,6)
    dice.append(dice_number)
print(dice)

def get_dice_number(number):
    for i in range(5):
        print(dice_art.get(number)[i])
        
        
請輸入要擲幾個骰子:3
[6, 4, 2]

step3.詢問骰幾次骰子執行顯示數字及圖案

import random

dice_art = {
    1: (
        "┌───────┐",
        "│       │",
        "│   ●   │",
        "│       │",
        "└───────┘",
    ),
    2: (
        "┌───────┐",
        "│ ●     │",
        "│       │",
        "│     ● │",
        "└───────┘",
    ),
    3: (
        "┌───────┐",
        "│ ●     │",
        "│   ●   │",
        "│     ● │",
        "└───────┘",
    ),
    4: (
        "┌───────┐",
        "│ ●   ● │",
        "│       │",
        "│ ●   ● │",
        "└───────┘",
    ),
    5: (
        "┌───────┐",
        "│ ●   ● │",
        "│   ●   │",
        "│ ●   ● │",
        "└───────┘",
    ),
    6: (
        "┌───────┐", # 0
        "│ ●   ● │", # 1
        "│ ●   ● │", # 2
        "│ ●   ● │", # 3
        "└───────┘", # 4
    ),
}
num_dice = int(input("請輸入要擲幾個骰子:"))
dice = []
for i in range(num_dice):
    dice_number = random.randint(1,6)
    dice.append(dice_number)
print(dice)

def get_dice_number(number):
    for i in range(5):
        print(dice_art.get(number)[i])

for i in dice:
    get_dice_number(i)


請輸入要擲幾個骰子:4
[3, 1, 5, 3]
┌───────┐
│ ●     │
│   ●   │
│     ● │
└───────┘
┌───────┐
│       │
│   ●   │
│       │
└───────┘
┌───────┐
│ ●   ● │
│   ●   │
│ ●   ● │
└───────┘
┌───────┐
│ ●     │
│   ●   │
│     ● │
└───────┘

函式 function
1.定義簡易函式

def say_hello():
    print("Hello World!")
say_hello()


Hello World!

2.呼叫函數時傳遞數據

def greeting(name):
    print(f"Hello, {name}!")
greeting("Jimmy")


Hello, Jimmy!

3.return語句用於結束函數的執行並將結果回傳給調用者 ex:加減乘除

def add(x,y):
    return x + y
result = add(3,5)
print(result)

def sub(x,y):
    return x - y
result2 = sub(5,2)
print(result2)

def mul(x,y):
    return x * y
result3 = mul(3,7)
print(result3)

def devide(x,y):
    return x / y
result4 = devide(15,3)
print(result4)


8
3
21
5.0

4.return語句用於結束函數的執行並將結果回傳給調用者 ex:姓名大寫

def create_name(first, last):
    first = first.capitalize()
    last = last.capitalize()
    return first + " " + last
print(create_name('meg', 'wick'))


Meg Wick

上一篇
Day 16: 猜數字遊戲 + 剪刀石頭布
下一篇
Day 18: 函式的預設引數 + 關鍵字參數
系列文
30天零基礎學習Python程式語言21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言