"Python" programming language Introduction Classic 100 cases--18

Source: Internet
Author: User

1 #题目: Find the value of S=A+AA+AAA+AAAA+AA...A, where a is a number. For example 2+22+222+2222+22222 (there are 5 numbers added at this time), several numbers are added with keyboard control.


Code:


2 3 num = int (input (' Enter the number that needs to be added: ')) 4 m = int (input (' Please enter the number of times to add: ')) 5 s = 0 6 for j in Range (1,m+1): 7 for I in Range (1,J+1): 8 s + = num* (10** (i-1)) 9 print (s)


Operation Result:


[[email protected] code_100]# python code_18.py Please enter the number you want to add: 3 Enter the number of times you want to add: 537035[[email protected] code_100]# python Code_ 18.py Enter the number you want to add: 7 Enter the number of times you want to add: 886419746[[email protected] code_100]# python code_18.py Enter the number you want to add: 2 Enter the number of times you want to add: 824691356[[email protected] code_100]# python code_18.py Enter the number you want to add: 8 Enter the number of times you want to add: 296


Code Explanation:


2 3 num = int (input (' Enter the number to add: ')) 4 m = int (input (' Please enter the number of times to add: ')) 5 s = 0 6 for j in Range (1,m+1): #遍历所有 Number to add 7 for I in Range (1,j+1): #遍历每一个数字的个十百千 .... Bit 8 S + = num* (10** (i-1)) #将所有的数字循环相加 9 print (s) #打印结果


This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1747427

"Python" programming language Introduction Classic 100 cases--18

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.