#!/usr/bin/python
# -*- coding: UTF-8 -*-
# exercise_8_2.py
def input_decimal(str):
while True:
num = raw_input(str)
if num and ((num[0]=='-' and unicode(num[1:]).isdecimal()) \
or unicode(num).isdecimal()):
return int(num)
else:
print "请输入实数!"
continue
def start():
f=input_decimal('from:')
t=input_decimal('to:')
i=input_decimal('increment:')
if f<=t and i>0:
print 'result:',range(f,t+1,i)
elif f>=t and i<0:
print 'result:',range(f,t-1,i)
else:
print '输入数据范围有误,结果为空!'
def showmenu():
print "功能:输入三个数字,得到一个列表"
prompt = """\
(s)tart
(q)uit
Enter choice: """
while True:
chosen = True
while chosen:
try:
choice = raw_input(prompt).strip()[0].lower()
except (EOFError, KeyboardInterrupt):
choice = 'q'
print '\nYou picked: [%s]' % choice
if choice not in 'sq':
print 'invalid option, try again'
else:
chosen = False
if choice =='q':
break
else:
start()
if __name__ == '__main__':
showmenu()
Python核心编程(第二版)练习8-2
最新推荐文章于 2021-12-23 17:03:54 发布