python核心编练习答案

2-2

a)print 1 + 2 *4


2-4

a)

text = raw_input('enter a word: ')
print text

b)

num = int(raw_input('enter a num: ')

print num


2-5

a)

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

b)

for i in range(0,11):
    print i

2-6

#-*- coding:utf-8 -*-

num = int(raw_input('请输入一个整数: '))
if num >0:
    print '%d 是正数' % num
elif num <0:
    print "%d 是负数" % num

else:
    print "这个数值是0"

2-7

for

#-*- coding:utf-8 -*-

text = raw_input('输入内容: ')

for i in range(len(text)):
    print i,text[i]

while

#-*- coding:utf-8 -*-

text = raw_input('输入内容: ')

i = len(text)

j = 0

while j < i:
    print j,text[j]

    j += 1


2-8

while

#coding=utf-8
i  = 0
alist = []
while i < 5:
        num = int(raw_input('enter a num: '))
        i += 1
        alist.append(int(num))
        num = sum(alist)
print num


for

# coding=utf-8


print sum(int(raw_input('输入一个整数:')) for i in range(5))

2-9

# coding=utf-8

print sum(float(raw_input('输入一个浮点数:')) for i in range(5)) / 5.0

2-10

# -*- coding:utf-8 -*-

print '请输入一个整数.'
count = 2
while count > 0:
    num = int(raw_input())

    if 1= 100:
            print "错了错了,数字太大"

        elif num <=1:
            print '错了错了,数字太小'

print 'Game Over'
            

2-11

#coding=utf-8
print """欢迎进入菜单程序,请选择下列序号:
		|--1 取五个数的和;
		|--2 取五个数的平均值;
		|--X 退出程序
		"""


choice = 0
while 1:
	choice = raw_input('请选择序号: ')
	if choice == 'x' or choice == 'X':
		break
	else:
		if choice == '1':
			print sum(int(raw_input('请输入一个整数:')) for i in range(5))
		elif choice == '2':
			print (sum(float(raw_input('请输入一个小数:')) for i in range(5))) / 5.0
		else:
			print '输入错误,请重新输入序号'



你可能感兴趣的:(python核心编练习答案)