Python核心编程v2.0 第二章习题答案

工具:pycharm

2.4

print "input a string"
str1 = raw_input() #接收用户键盘输入
print str1

print  "input a number"
str2 = int(raw_input()) #转化为int类型
print str2

2.5

i = 0
while i <= 10 :
    print i,
    i = i+1

print '\n'

for j in range(11) : #注意range为11
    print j,

2.6

x = int(raw_input())
if x > 0 :
    print "positive"
elif x < 0 :
    print "negative"
else:
    print "x = 0"

2.7

print "input a string"
x = raw_input()
for i in range(len(x)) :
    print x[i],

print '\n'

j = 0
while j < len(x) : #此处应无等号
    print x[j],
    j = j+1

2.8

list = [9,8,7,6,5]
sum = 0

i = 0
while i < len(list) :
    sum = sum + list[i]
    i = i + 1
print  sum

sum = 0
for j in range(len(list)) :
    sum = sum + list[j]
print sum

#接收用户输入的5个数,存入list并求和
count = 0
yoursum = 0
while count < 5 :
    print "input a number"
    list[count] = int(raw_input())
    yoursum = yoursum + list[count]
    count = count + 1
print "your list is ",
print list
print "your sum is ",
print yoursum

2.9

from __future__ import division #floor除法
list = [1,8,1,6,10]
sum = 0

i = 0
while i < len(list) :
    sum = sum + list[i]
    i = i + 1
print  sum

print sum/5

2.10

i = True
while i == True :
    print "input a number between 0-100"
    x = int(raw_input())
    if 0<x<100 :
        print "ok"
        i = False
    else:
        print "not suit"

2.11

from __future__ import division
def sum() :
    list = []
    count = 0
    yoursum = 0
    while count < 5 :
        print "input a number"
        list.append(int(raw_input()))
        yoursum = yoursum + list[count]
        count = count + 1
    print "your list is ",
    print list
    print "your sum is ",
    print yoursum

def ave() :
    list = []
    count = 0
    yoursum = 0
    while count < 5 :
        print "input a number"
        list.append(int(raw_input()))
        yoursum = yoursum + list[count]
        count = count + 1
    print "your list is ",
    print list
    print "your ave is ",
    print yoursum/5

i = True
while i == True :
    print "1.sum 5 number"
    print "2.average 5 number"
    print "x.quit"
    print '----------------------------'
    print "input order"
    order = raw_input()
    if order == 'x' :
        print "over"
        i = False
    elif int(order) == 1 :
        sum()
    elif int(order) == 2 :
        ave()
    else:
        print "not an order"

2.15

print "input three number"
x = int(raw_input())
a = int(raw_input())
b = int(raw_input())

if  a<b<x :
    print a,b,x
elif a<x<b :
    print a,x,b
elif b<a<x :
    print b,a,x
elif b<x<a :
    print b,x,a
elif x<b<a :
    print x,b,a
else:
    print x,a,b
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值