- 1 自己編寫一個乘法表,提示使用人輸入一個數字,并輸出乘法表。
如下
print "Which multiplication table would you like?"
a =int(raw_input())
print "Here's your table:"
for i in range(1,11):
print a ,'*',i,'=',a*i
run
Which multiplication table would you like?
5
Here's your table:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50```
######總結:
raw_input()函數輸出 str ,需要用int()轉換。
- 2 試著用while循環 完成上題
print "Which multiplication table would you like?"
a =int(raw_input())
print "Here's your table:"
i=1
while i<11:
print a ,'',i,'=',ai
i = i+1
*Run*
Which multiplication table would you like?
5
Here's your table:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50```
總結:
這題難道我了,還好我想起了以前做過類似的循環練習,想到了自增 i = i+1 .