Python core programming, Version 2, 160th, page 6, chapter 6 Exercise continued 3-answers to Python core programming-self-developed-

Source: Internet
Author: User

6-7.
Debugging. Let's take a look at the code (buggy. py) in example 6.5)
(A) Study this code and describe what it is intended to do. Enter your comments in all.
(B) This program has a big problem. For example, Input 6, 12, 20, and 30 will die. In fact, it cannot process any even numbers and find out the cause.
(C) Amendment (B.
Example 6.5 programs with bugs (buggy. py)
This is a program used to practice 6-7. to determine what the program is doing, add your comments in "#" to find out the errors and modify them.
Num_str = raw_input ('enter a number :')
Num_num = int (num_str)
Fac_list = range (1, num_num + 1)
Print "BEFORE:", 'fac _ list'
I = 0

While I <len (fac_list ):
If num_num % fac_list [I] = 0:
Del fac_list [I]
I = I + 1

Print "AFTER":, 'fac _ list'
[Answer]
The purpose of this Code is to enter a natural number, create an incremental list based on the number, and check whether the natural number can be divisible by the elements in the list one by one. If yes, delete the list element from the list.
The modified code is as follows:
Num_str = raw_input ('enter a number :')
Num_num = int (num_str)
Fac_list = range (1, num_num + 1)
Print "BEFORE:", fac_list

I = 0

While I <len (fac_list ):
If num_num % fac_list [I] = 0: # Check whether the input number can be divisible by an element in the list.
Del fac_list [I]
I = I + 1

Print "AFTER:", fac_list
[Execution result]
Enter a number: 12
BEFORE: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
AFTER: [2, 4, 5, 7, 8, 9, 10, 11]

6-8.
List. An integer value is given, and the return value indicates the value in English. For example, if the input is 89, "eight-nine" is returned ". Additional question: a new style that conforms to the English syntax rule can be returned. For example, if you enter 89, "eighty-nine" is returned ". The value in this exercise is assumed to be 0 ~ 1000.
[Answer]
The Code is as follows:
Number = int (raw_input ('Please input a number between 1 to 1000 :...'))

Units = ['zero ', 'one', 'two', 'three', 'four ', 'five', 'six', 'seven', 'Eight ', 'nine']
Tens_units = ['ten ', 'even', 'twelve', 'thirteen', 'Fourteen', 'deleteen', 'sixteen', 'seventeen', 'eighteen ', 'nineten']
Tens = ['twenty', 'Thirty ', 'forty', 'ty ty ', 'sixty', 'seventy', 'hthty', 'ninety']

Print 'the number you input is :'

If 0 <= number <= 9: print units [number]
Elif 10 <= number <= 19: print tens_units [number-10]
Elif 20 <= number <= 99:
If number % 10 = 0: print tens [number/10-2]
Else: print '% s-% s' % (tens [number/10-2], units [number % 10])
Elif 100 <= number <= 999:
If number % 100 = 0: print '% s hundred' % (units [number/100])
Elif number % 10 = 0 and (number-100*(number/100 ))! = 10: print '% s hundred and % s' % (units [number/100], tens [(number-100 * (number/100)/10-2])
Elif number % 10 = 0 and (number-100 * (number/100) = 10: print '% s hundred and ten' % (units [number/100])
Elif number % 10! = 0 and 20 <(number-100 * (number/100) <= 99: print '% s hundred and % s-% s' % (units [number/100], tens [(number-100 * (number/100)/10-2], units [number-10 * (number/10)])
Elif number % 10! = 0 and 10 <(number-100 * (number/100) <20: print '% s hundred and % s' % (units [number/100], tens_units [number-100 * (number/100)-10])
Elif number % 10! = 0 and 0 <(number-10 * (number/10) <10: print '% s hundred and % s' % (units [number/100], units [number-10 * (number/10)])
Else: print 'ten hundred'
[Execution result]
Please input a number between 1 to 1000:... 309
The number you input is:
Three hundred and nine

Please input a number between 1 to 1000:... 721
The number you input is:
Seven hundred and twenty-one

6-9.
Conversion. Write a sister function for exercise 5-13, which accepts the number of minutes, returns the number of hours and the number of minutes. The total time remains unchanged, and the hour is required to be as large as possible.
[Answer]
The Code is as follows:
Minutes = int (raw_input ('Please input the minutes :...'))
Hours = minutes/60
Mins = minutes-hours * 60
Print "% I: % I" % (hours, mins)

6-10.
String. Write a function to return a string similar to the input string, which requires case-sensitivity inversion. For example, if "Mr. Ed" is input, "mR. eD" should be returned as the output.
[Answer]
The Code is as follows:
Input = raw_input ('Please input a string :...')
Output =''
For I in input:
If I = I. upper ():
Output = output + I. lower ()
Else:
Output = output + I. upper ()
Print output

Keywords: python core programming answers unofficial blog

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.