- 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 .