#HW期中考
#請執行此 cell
#載入函式庫 --- 下列程式請勿修改
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
from IPython.display import display, Markdown # 載入disply函式庫
import pandas as pd
import random # 載入亂數函式庫
import os
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# 請在 Student_no 填入你的學號,範例 : student_no = '12345678'
# =======================================================================
student_no = "410732063" # ""中填入你的學號
# =======================================================================
#學號檢查 --- 下列程式請勿修改
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (student_no == ""):
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(" 請在 Student_no 填入你的學號")
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
else:
print("=================================")
print(f‘410732063 {student_no}")
print("=================================")
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
add Codeadd Markdown
[ ]:
'''第一題 解一元二次方程式'''
'''
輸入 一元二次方程式 ax2+bx+c 的三個係數
請檢查輸入的正確性
請 列出 公式解 無解 重根
程式請加上註解
'''
'''請在此開始作答'''
import math
a=float(input("a=:"))
b=float(input("b=:"))
c=float(input("c=:"))
delta=b*b-4*a*c
if delta<0:
print(" x 無解")
elif delta==0:
print(" x有重根",-b/(2*a))
else:
print("x 有根",(-b+math.sqrt(delta))/(2*a),(-b-math.sqrt(delta))/(2*a))
add Codeadd Markdown
[ ]:
print(math.sqrt(4))
add Codeadd Markdown
第一題 解一元二次方程式
相關說明
https://zh.wikipedia.org/wiki/%E4%B8%80%E5%85%83%E4%BA%8C%E6%AC%A1%E6%96%B9%E7%A8%8B
輸入 一元二次方程式 ax2+bx+c 的三個係數
請檢查輸入的正確性
請 列出 公式解 無解 重根
程式請加上註解
下面是係數和輸出的範例
a = 1, b = -1, c = -30
>> x 有根 6, -5
a = 4, b = 4, c = 1
>> x 有重根 -0.5
a = 1, b = 1, c = 5
>> x 無解
add Codeadd Markdown
第二題 大樂透 對獎程式
相關說明
大樂透是一種樂透型遊戲。您必須從01~49中任選6個號碼進行投注。開獎時,開獎單位將隨機開出六個號碼加一個特別號,這一組號碼就是該期大樂透的中獎號碼,也稱為「獎號」。您的六個選號中,如果有三個以上(含三個號碼)對中當期開出之六個號碼(特別號只適用於貳獎、肆獎、陸獎和柒獎),即為中獎,並可依規定兌領獎金。
各獎項的中獎方式如下表:
Image
add Codeadd Markdown
[ ]:
'''第二題題目,請執行此 cell --- 下列程式請勿修改'''
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
random.seed(student_no)
rewards = {0:"頭獎", 1:"參獎"}
print(f"第二題題目")
print("===========================================================")
print(f"請寫一個可對大樂透 {rewards[random.randint(0,1)]} 的對獎程式")
#-------------------------------
# generate 中獎號碼
win_no = []
while len(win_no) != 6:
no = random.randint(1,49)
if no not in win_no:
win_no.append(no)
print(f"中獎號碼: {win_no}")
#-------------------------------
print("===========================================================")
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def num_generate():
'''
generate 任選 6 個號碼 selec_no
'''
select_no = win_no.copy()
# 先去掉中獎號碼中 0-1 個號碼
for i in range(0, random.randint(0,2)):
select_no.pop()
# 再加人其他的號碼
while len(select_no) != 6:
no = random.randint(1,49)
if (no not in select_no) and (no not in win_no):
select_no.append(no)
return select_no
add Codeadd Markdown
[ ]:
'''第二題請在此開始作答'''
def chk_no(select_no):
'''
對獎程式
輸入任選6個號碼 selected_no
題目已給中獎號碼 win_no
回傳中獎結果 “沒中” “頭獎” "參獎"
'''
n_corect = 0
for i in range(6):
no = select_no[i]
if no in win_no:
n_corect += 1
if n_corect == 6:
return "頭獎"
elif n_corect == 0:
return "沒中"
else :
return "參獎"
# 測試程式
select_no = num_generate()
res = chk_no(select_no)
print(f"第二題測試")
print(f"中獎號碼: {win_no}")
print(f"任選號碼: {select_no} 中獎結果 {res}")
print("===============================================")
add Codeadd Markdown
第三題 密碼檢測
相關說明
請設計一個程式,輸入一段密碼之後,可以檢查輸入的密碼是否符合以下幾點:
長度介於8~20個字
只能包含英文字母、數字、以及以下符號「!@#$%^&*()_+」
必須包含至少一個大寫英文字母
必須包含至少一個小寫英文字母
必須包含至少一個數字
必須包含至少一個符號
add Codeadd Markdown
若密碼符合規則,輸出「密碼有效」。
若密碼不符合規則,輸出「密碼無效」,以及缺少的規則。
以下為範例:
輸入1:
Joe9487520
輸出1:
密碼無效,必須包含至少一個符號。
輸入2:
!Joe9487520
輸出2:
密碼有效。
輸入3:
aaaaaa123445
輸出3:
密碼無效,必須包含至少一個大寫英文字母,必須包含至少一個符號。
add Codeadd Markdown
Expand
add Codeadd Markdown
[ ]:
'''
第三題請在此開始作答,程式請加上註解
'''
goodPass = False
while not goodPass:
password = input("create a password:")
symbols = "!@#$%^&*()_+'"
len_error = False
has_num = False
has_upper = False
has_lower = False
has_symb = False
has_errorsymb = False
error_symb = []
if len(password) < 8 or len(password) > 20: #如果密碼小於 8 或是大於 20
len_error = True
for i in range(len(password)):
pchar = password[i]
if pchar.isdigit():
has_num = True
#print(f"{pchar} {has_num}")
elif pchar.isalpha():
if pchar.isupper():
has_upper = True
elif pchar.islower():
has_lower = True
#print(f"{pchar} {has_upper} {has_lower}")
else :
chk_sym = 0
for s in range(len(symbols)):
symb = symbols[s]
if pchar == symb:
chk_sym = 1
has_symb = True
#print(f"{pchar} {has_symb}")
if chk_sym == 0:
has_errorsymb = True
error_symb.append(pchar)
#print(f"{pchar} a strange 符號")
goodPass = (not len_error) and (not has_errorsymb) and has_num and has_upper and has_lower and has_symb
if goodPass:
print(f"{password} is a good password")
else:
print(f"{password} is not a good password")
if len_error:
print("長度沒有介於8~20個字") #rule1 無效
if has_errorsymb:
print(f"{error_symb} is a strange symbol")
if not has_upper:
print('必須包含至少一個大寫英文字母')
if not has_lower:
print('必須包含至少一個小寫英文字母')
if not has_num:
print('必須包含至少一個數字')
if not has_symb:
print('必須包含至少一個符號「!@#$%^&*()_+」')
add Codeadd Markdown
第四題 猜數字
相關說明
猜數字是一個簡單好玩的遊戲,關主選擇一個四位數字(數字不會重複),然後給闖關者猜一個四位數字。
關主就會依照數字關係,反饋結果。
規則如下:
若數字正確,位置也正確,就是A。
若數字正確,位置錯誤,就是B。
Correct_number = 2468
2135
範例輸出1:
1A0B
範例輸入2:
2635
範例輸出2:
1A1B
範例輸入3:
2864
範例輸出3:
2A2B
範例輸入4:
2468
範例輸出4: 4A0B
add Codeadd Markdown
[ ]:
''' 第四題題目,請執行此 cell --- 下列程式請勿修改'''
#=========================================
random.seed(student_no)
# 不重複四個數字
c_no = []
while len(c_no) != 4:
no = random.randint(1, 9)
if no not in c_no:
c_no.append(no)
#print(c_no)
# 不重複四位數字
correct_no = 0
for i, no in enumerate(c_no):
correct_no += no*10**(3-i)
#=======================================
print(f"第四題猜數字")
print("========================")
print(f"要猜的數字 {correct_no}")
print(f"請完成猜數字的遊戲")
add Codeadd Markdown
[ ]:
'''
第四題請在此開始作答,程式請加上註解
要猜的數字 ==〉 correct_no
'''
number = input("enter a number of 4 digit:")
no_str = str(correct_no)
na = 0
nb = 0
for i in range(4):
if number[i] in no_str:
if number[i] == no_str[i]:
na +=1
else : nb +=1
print("範例輸出:",f"{na}A{nb}B")
https://www.kaggle.com/quynhmilk97/notebook6e02b3542b/edit/run/80115047