Python core Programming The answer to the second edition of the exercises

Source: Internet
Author: User

2-5

#写一个while循环, the output integral type is 0~10

A=0
While a<11:
Print a
A+=1

#写一个for循环重复以上操作

For I in range (11):
Print I

2-6

#条件判断, determine whether a number is positive or negative, or 0. Start with a fixed value, then modify your code to support user input values to determine

A=int (raw_input ("Please input a number:"))
#a =6
If a>0:
Print "This number is positive!"
Elif a<0:
Print "This number is negative!"
Else
Print "This number is zero!"

2-7

#从用户那里接受一个字符串输入, and then displays the string verbatim, using the For loop implementation

A=raw_input ("Please input a string:")
For I in A:
Print I

#同样再用while循环实现上述

A=raw_input ("Please input a string:")
I=0
While I<len (a):
Print A[i]
I+=1

2-8

#创建一个包含五个固定数值的列表或元祖, output their and then modify the code to accept user input values, using the while and for loops respectively

The simplest algorithm for #print (reduce (lambda x,y:x+y,range (1,5)))

#for循环:

A=input ("Please input a list or a tuple:")
#a =[1,2,3,4]
S=0
For I in range (Len (a)):
S+=a[i]
Print S

#while循环:

A=input ("Please input a list or a tuple:")
#a =[1,2,3,4]
I=0
S=0
While I<len (a):
S+=a[i]
I+=1
Print S

2-9 loops and operators, create a list or tuple with five fixed values, output their average

#for循环

a=[1,2,3]
S=0
For I in range (Len (a)):
S+=a[i]
Print float (S/len (a))

#while Cycle

A=input ("Please input a list or a tuple:")
I=0
S=0
While I<len (a):
S+=a[i]
I+=1
Print float (S/len (a))

2-10 user input with loop and conditional judgment, using the Raw_input () function to prompt the user for a number between 1 and 100, if the number of user input satisfies this condition, the display succeeds and exits, otherwise displays an error message and then prompts the user to enter a value again, knowing that the condition is met.


While 1:
A=int (raw_input ("Please input a number:"))
If 1<=a<=100:
Print "success!"
Break
Else
print "Input Error!plese input again!"
Continue

2-11

#写一个带文本菜单的程序, the menu items are as follows: (1) Take 5 numbers and (2) take the average of 5 numbers; (X) exit, the user makes a selection, then performs the corresponding function

Def Menu ():
print "1.get sum of five numbers"
Print "2.get average of five numbers"
Print "X.exit"

Def Sum ():
l=[1,2,3,4,5]
Sum=0
For I in L:
Sum+=i
I+=1
return sum
Def Ave ():
Sum=sum ()
Ave=float (SUM)/5
Return Ave
While True:
Menu ()
Choice=input ("Please choice One:")
If choice==1:
Sum=sum ()
Print "sum is%d"%sum
Elif choice==2:
Ave=ave ()
Print "average is%f"%ave
Elif choice== ' x ':
Break
Else
Print "Wrong choice,please choose again!"

2-15

#让用户输入3个数值并将分别将他们保存到3个不同的变量中, do not use a list or sorting algorithm
#从小到大
While True:
Num1=int (raw_input ("Please input NUM1:"))
Num2=int (raw_input ("Please input num2:"))
Num3=int (raw_input ("Please input num3:"))
If num1>num2:
Tmp=num1
Num1=num2
Num2=tmp
If num2>num3:
Tmp=num2
Num2=num3
Num3=tmp
If num1>num2:
Tmp=num1
Num1=num2
Num2=tmp
Print "from Min to Max is%d,%d,%d"% (NUM1,NUM2,NUM3)
#从大到小:
While True:
Num1=int (raw_input ("Please input NUM1:"))
Num2=int (raw_input ("Please input num2:"))
Num3=int (raw_input ("Please input num3:"))
If num1<num2:
Tmp=num1
Num1=num2
Num2=tmp
If num2<num3:
Tmp=num2
Num2=num3
Num3=tmp
If num1<num2:
Tmp=num1
Num1=num2
Num2=tmp
Print "from Max to Min is%d,%d,%d"% (NUM1,NUM2,NUM3)

Python core Programming The answer to the second edition of the exercises

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.