Python core programming version 2, 36th page, Chapter 2 exercise continued 1-answers to Python core programming-self-developed-

Source: Internet
Author: User
Tags integer division

2-6.
Condition judgment. Determine whether a number is positive or negative, or 0. Start with a fixed value, and then modify your code to support the user to enter a value for determination.
[Answer]
The Code is as follows:
A = float (raw_input ("Please input a number ..."))
If a = 0:
Print "The number you input is Zero"
Elif a> 0:
Print "The number you input is Positive"
Else:
Print "This is a negative number"

2-7.
Loop and string. Receives a string input from the user, and displays the string by character. First implement with a while loop and then use a for loop.
[Answer]
The Code is as follows:
A = raw_input ("Please input a string ...")
Print 'display in for loop :'
For I in:
Print I,
Print '\ nDisplay in while loop :'
J = 0
While (j <len ()):
Print a [j]
J = j + 1

2-8.
Loops and operators. Create a list or tuples containing five fixed values to output their sums. Then modify your code to accept user input values. Use the while and for loops respectively.
[Answer]
The Code is as follows:
# Using while loop
I = 0
Total = 0
A = [1, 2, 3, 4, 5]
While (I <5 ):
Print 'Please input number', I + 1
A [I] = float (raw_input ())
Total = total + a [I]
I = I + 1
Print 'the total is ', total

# Using for loop
Total = 0
A = [1, 2, 3, 4, 5]
For I in range (0, 5 ):
Print 'Please input number', I + 1
A [I] = float (raw_input ())
Total = total + a [I]
Print 'the total is ', total

2-9.
Loops and operators. Create a list or tuples containing five fixed values and output their average values. One of the difficulties in this exercise is to obtain the average value through division. You will find that Integer Division takes decimal places, so you must use floating point division to get more accurate results. Float () built-in functions can help you implement this function.
[Answer]
The Code is as follows:
I = 0
Total = 0
A = [1, 2, 3, 4, 5]
While (I <5 ):
Print 'Please input number', I + 1
A [I] = float (raw_input ())
Total = total + a [I]
I = I + 1
Print 'The average is ', total/5.

# Using for loop
Total = 0
A = [1, 2, 3, 4, 5]
For I in range (0, 5 ):
Print 'Please input number', I + 1
A [I] = float (raw_input ())
Total = total + a [I]
Print 'The average is ', total/5.

2-10.
User input with loop and condition judgment. Use the raw_input () function to prompt the user to enter a number between 1 and 100. If the value entered by the user meets this condition, it is displayed as successful and exits. Otherwise, an error message is displayed, prompting the user to enter a value until the conditions are met.
[Answer]
The Code is as follows:
T = 1
While (t ):
A = float (raw_input ('Please input a number between 1 and 100 :...'))
If a <100 and a> 1:
Print 'your have input a number between 1 and 100 .'
T = 0
Else:
Print 'Please try again ...'
[Unfinished] the input is not a number.

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

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.