s40723148 作業網站

Python 語法 << Previous Next >> Python 手冊

選號器

老師原本的選號器

# 大樂透電腦選號
# lottery
from browser import document, html, alert
import random

try:
    total = int(input("請問要出幾張彩卷號碼?"))
except:
    alert("請輸入要選擇電腦選號數量的'整數'")
    total = int(input("請問要出幾張彩卷號碼?"))

# 準備將電腦選出的號碼, 輸出到內定 id="brython_div" 的標註區域
output_div = document["brython_div"]
output_div <= "以下將出 " + str(total) + " 張電腦選號彩卷:" + html.BR()
for i in range(1, total + 1):
    # 利用 list(range()) 產生 1 到 49 的 population list
    # 然後再透過 random.sample(population, k) 
    # 從 population, 產生 k 個不同的數字
    numbers = random.sample(list(range(1, 49)), 6)
    output_div <= str(i) + ". 電腦選號為: " + str(numbers) + html.BR()

我們進行改版後的選號器

from browser import document, html, alert
import random

n = int(input("是否指定號碼,是請輸入 0 否請輸入 1"))
if (n == 1):
    try:
        total = int(input("請問要出幾張彩卷號碼?"))
    except:
        alert("請輸入要選擇電腦選號數量的'整數'")
        total = int(input("請問要出幾張彩卷號碼?"))
        
    print("以下將出 " + str(total) + " 張電腦選號彩卷:")
    for i in range(1, total + 1):
    # 利用 list(range()) 產生 1 到 49 的 population list
    # 然後再透過 random.sample(population, k) 
    # 從 population, 產生 k 個不同的數字
        numbers = random.sample(list(range(1, 100)), 10)
        print(str(i) + ". 電腦選號為: " + str(numbers))
    print("預祝您中獎!")
if (n == 0):
    try:
        total = int(input("請問要出幾張彩卷號碼?"))
    except:
        alert("請輸入要選擇電腦選號數量的'整數'")
        total = int(input("請問要出幾張彩卷號碼?"))

    try:
        idd = int(input("請輸入請輸入自選號"))
        
    except:
        alert("請輸入自選號'整數'")
        idd = int(input("請輸入自選號")) 
    if (0 < idd < 101):
        print("以下將出 " + str(total) + " 張電腦選號彩卷:")
        for i in range(1, total + 1):
            numbers = random.sample(list(range(1, 100)), 9)
            print(str(i) + ". 電腦選號為: " + str(numbers) + ". 指定號為: " + str(idd))
        print("預祝您中獎!")
    if (idd > 100 or idd<1):
        try:
            ice = int(input("請輸入自選號'1~100'"))
        
        except:
            alert("請輸入自選號''1~100'")
            ice = int(input("請輸入自選號'1~100'")) 
        print("以下將出 " + str(total) + " 張電腦選號彩卷:")
        for i in range(1, total + 1):
            numbers = random.sample(list(range(1, 100)), 9)
            print(str(i) + ". 電腦選號為: " + str(numbers) + ". 指定號為: " + str(ice))
        print("預祝您中獎!")  
    
        
        
    


    
    
    

我們在改版中的選號器加入了可以指定號碼的功能

測試影片


Python 語法 << Previous Next >> Python 手冊