python打印数字塔图形_用算盘图形表示数字,Python实现

1 #########################################################################

2 #10-row School abacus

3 #by

4 #Michael H

5 #########################################################################

6 #Description partially extracted from from wikipedia

7 #8 #Around the world, abaci have been used in pre-schools and elementary

9 #10 #In Western countries, a bead frame similar to the Russian abacus but

11 #with straight wires and a vertical frame has been common (see image).

12 #Helps schools as an aid in teaching the numeral system and arithmetic

13 #14 #|00000***** | row factor 1000000000

15 #|00000***** | row factor 100000000

16 #|00000***** | row factor 10000000

17 #|00000***** | row factor 1000000

18 #|00000***** | row factor 100000

19 #|00000***** | row factor 10000

20 #|00000***** | row factor 1000

21 #|00000**** *| row factor 100 * 1

22 #|00000*** **| row factor 10 * 2

23 #|00000** ***| row factor 1 * 3

24 #-----------

25 #Sum 123

26 #27 #Each row represents a different row factor, starting with x1 at the

28 #bottom, ascending up to x1000000000 at the top row.

29 ######################################################################

30

31 #TASK:

32 #Define a procedure print_abacus(integer) that takes a positive integer

33 #and prints a visual representation (image) of an abacus setup for a

34 #given positive integer value.

35 #

36 #Ranking

37 #1 STAR: solved the problem!

38 #2 STARS: 6 < lines <= 9

39 #3 STARS: 3 < lines <= 6

40 #4 STARS: 0 < lines <= 3

41

42 defprint_abacus(value):43 row_number = 1

44 while row_number <= 10:45 number = value // (10**(10-row_number))46 if number ==0:47 print ‘|‘ + ‘0‘*5 + ‘*‘*5 + ‘ ‘*3 + ‘|‘

48 elif number > 0 and number < 5:49 print ‘|‘ + ‘0‘*5 + ‘*‘*(5-number) + ‘ ‘*3 + ‘*‘*number + ‘|‘

50 else:51 print ‘|‘ + ‘0‘*(10-number) + ‘ ‘*3 + ‘0‘*(number-5) + ‘*‘*5 + ‘|‘

52 value -= number*(10**(10-row_number))53 row_number += 1

54

55

56

57

58

59 ### TEST CASES

60 print "Abacus showing 0:"

61 print_abacus(0)62 #>>>|00000***** |

63 #>>>|00000***** |

64 #>>>|00000***** |

65 #>>>|00000***** |

66 #>>>|00000***** |

67 #>>>|00000***** |

68 #>>>|00000***** |

69 #>>>|00000***** |

70 #>>>|00000***** |

71 #>>>|00000***** |

72 print "Abacus showing 12345678:"

73 print_abacus(12345678)74 #>>>|00000***** |

75 #>>>|00000***** |

76 #>>>|00000**** *|

77 #>>>|00000*** **|

78 #>>>|00000** ***|

79 #>>>|00000* ****|

80 #>>>|00000 *****|

81 #>>>|0000 0*****|

82 #>>>|000 00*****|

83 #>>>|00 000*****|

84 print "Abacus showing 1337:"

85 print_abacus(1337)86 #>>>|00000***** |

87 #>>>|00000***** |

88 #>>>|00000***** |

89 #>>>|00000***** |

90 #>>>|00000***** |

91 #>>>|00000***** |

92 #>>>|00000**** *|

93 #>>>|00000** ***|

94 #>>>|00000** ***|

95 #>>>|000 00*****|

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值