Python core programming version 2 Chapter 5 exercise continued 1-Python core programming answers-self-developed-

Source: Internet
Author: User
Tags integer division

The answer here is not from official resources, but from my own exercises, which may be incorrect.

5-5.
Return the remainder. Take any amount less than $1 and calculate the minimum number of coins you can replace. There are four types of coins: 1 cent, 5 cent, 10 cent, and 25 cent. $1 equals 100 cents. For example, the calculation result of $0.76 is 3, 25, and 1, respectively. Similar to 76 items, 1 cent, 2 items, 25 cents, 2 items, 10 cents, 1 item, 5 cents, and 1 piece, the results do not meet the requirements.
[Answer]
The Code is as follows:
Def verify ():
B = '000000'
If len (a) = 2 and a [0] in B and a [1] in B: return True
Else: return False

Def change ():
N25 = int (a)/25
N10 = (int (a)-25 * N25)/10
N5 = (int (a)-25 * N25-10 * N10)/5
N1 = int (a)-25 * N25-10 * N10-5 * N5
Print '% I cents can be changed to:' % int ()
Print '25 Cents: % I '% N25
Print '10 Cents: % I '% N10
Print '5 Cents: % I '% N5
Print '1 Cents: % I '% N1

A = raw_input ("Please input the number in U. S cents :...")
If verify (a): change ()
Else: print "Invalid input, Please try again ."

[Recommendation] I like the following two articles, so I recommend them.
Http://linzhong2006.javaeye.com/blog/205270
The struggle of a programmer

Http://www.cnblogs.com/netgarden/archive/2007/09/07/886132.html
The growth history of an excellent IT expert

5-6.
Arithmetic. Write a calculator program. Your code can accept this expression. two operands are added with an operator N1 operator N2. Among them, N1 and N2 are integer or floating point type, and the operators can be +,-, *,/, % ,**, addition, subtraction, multiplication, integer division, remainder operation, and power operation. Calculate the result of this expression and display it. Tip: You can use the string method split (), but you cannot use the built-in function eval ().
[Answer]
The Code is as follows:
Print 'Please input the expression, such as 1 + 23 :...'
Expression = raw_input ()

Def verify ():
Try:
Float ()
Return True
Failed t ValueError:
Print 'you input an illegal expression. "% s" is not a number. '%
Return False

If len (expression. split ('+') = 2:
If (verify (expression. split ('+') [0]) and (verify (expression. split ('+') [1]):
Print '= % F' % (float (expression. split (' + ') [0]) + float (expression. split (' + ') [1])
Pass

Elif len (expression. split ('*') = 2:
If (verify (expression. split ('*') [0]) and (verify (expression. split ('*') [1]):
Print '= % F' % (float (expression. split (' * ') [0]) * float (expression. split (' * ') [1])
Pass

Elif len (expression. split ('/') = 2:
If (verify (expression. split ('/') [0]) and (verify (expression. split ('/') [1]):
Print '= % F' % (float (expression. split ('/') [0])/float (expression. split ('/') [1])
Pass

Elif len (expression. split ('%') = 2:
If (verify (expression. split ('%') [0]) and (verify (expression. split ('%') [1]):
Print '= % F' % (float (expression. split (' % ') [0]) % float (expression. split (' % ') [1])
Pass

Elif len (expression. split ('**') = 2:
If (verify (expression. split ('**') [0]) and (verify (expression. split ('**') [1]):
Print '= % F' % (float (expression. split ('**') [0]) ** float (expression. split ('**') [1])
Pass

Elif len (expression. split ('-') = 2:
If (verify (expression. split ('-') [0]) and (verify (expression. split ('-') [1]):
Print '= % F' % (float (expression. split ('-') [0])-float (expression. split ('-') [1])
Pass

Else:
Pass
[Unfinished] This program cannot calculate an expression such as 1-2.

5-7.
Business Tax. Take a commodity amount at will, and then calculate the business tax payable based on the local business tax quota.
[Answer]
The Code is as follows:
PurePrice = float (raw_input ('Please input the price :...'))
Print 'you should pay :'
Print 'subtotal: % 10.2f '% purePrice
Print 'sales Tax: % 10.2f '% round (purePrice * 0.11, 2)
Print 'total: % 10.2f '% (purePrice + round (purePrice * 0.11, 2 ))

[Recommendation] I like the blog "indifferent Rivers and Lakes. NET program life", so I recommend it.
Http://www.cnblogs.com/zzc315/category/61542.html

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.