2011年10月19日 星期三

100設計一甲_week5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#print()<---在python 3裡print是一個函數 所以必須加上括號 與之前python版本print不同
#input()<---輸入,輸入進來的型態為str
#str()<---將()內轉換成字串
def ex1():
yourname = input("Please input your English name:")
for i in range(0,4):
print(yourname[i]+" ASCII value is "+str(ord(yourname[i])))
#輸入4字元的字串
#ord()轉換()內字元所對應的ascii瑪
#chr()轉換()內數字所對應的字元<---須為int
def ex2():
var = input("Please enter a four character string:")
print("The string capitalized is ",end="")
for i in range(0,4):
print(chr(ord(var[i])-32),end="")
print()
#mpg mile per gallon
#求每一加侖所跑的里程
#輸入miledorve、gallon
#miledorve/gallon=每加侖所跑的miles
def ex3():
miledrove = float(input("Please enter the miles you drove:"))
gallons = float(input("Please enter the gallons \
of gas you put in the tank:"))
print("You got",str(miledrove/gallons),"mpg on that tank of gas.")
#美元與外國幣值稅換
#以美國為主所以要查美國相對於各國的匯率
#rate此函式中代表匯率
#結果應該是 所要稅換的錢幣*匯率
def ex4():
dollars = float(input("What is the amount of US \
Dollars you wish to convert? "))
rate = float(input("What is the current exchange \
rate (1~US Dollar equals what in the Foreign Currency)? "))
print("The amount in the Foreign Currency is $%.2f"%(dollars*rate))
#以list來做9*9乘法表
#必須清楚for迴圈內的動作,變數的變動
#了解如何存取list內的內容
def ex5():
N=list(range(1,10))#[1,2...9]
X=list(range(1,10))#[1,2...9]
for i in N:
for y in X:
print(i,"*",y,"=",i*y,end="\t")
print()
def ex6():
N=list(range(20,31))#[20,21,22...30]
X=list(range(20,31))#[20,21,22...30]
for i in N:
for y in X:
print("%3d * %3d =%4d"%(i,y,i*y),end="\t")
print()
#店員找錢
#使用while迴圈 應找的錢(change) dollars(各種錢幣) pay(應找的硬幣數)
#每一次迴圈 把當時所要找的錢除目前使用的硬幣 得到其商 就是這種硬幣要找的數量
#找完一次,必須要把找的錢與已給的錢(硬幣直*找的硬幣數量)相減
#迴圈直到要找的錢為0時,結束迴圈
def pay():
change=int(input("input how much you should pay:"))
dollars=[100,50,10,5,1]
pay=[0,0,0,0,0]
i=0
while(change!=0):
coin=change//dollars[i]
pay[i]=coin
change=change-coin*dollars[i]
i+=1
print(pay)
for num in range(0,5):
print("%3d : %d 張"%(dollars[num],pay[num]))
pay()

沒有留言:

張貼留言