python-如何根据用户选择实现不同函数功能
2017-04-12 16:34
781 查看
由于本人python小白一个,目前在看《python 核心编程第二版》,以下代码大部分来自 https://wenku.baidu.com/view/4250ebaaa417866fb94a8e58.html.
题目如下:写一个带文本菜单的程序,菜单项如下:(1)取五个数的和;(2)取5个数的平均值 (x)退出。由用户做一个选择,然后执行相应的功能。
def sum():
total=0
for i in range(5):
print"please input number%d"%(i+1)
a=input()
total=total+a
print"total=",total
def average():
total=0
for i in range(5):
b=float(input('please enter a number%d:'%(i+1)))
total=total+b
print'the average is',total/5
print'''
please input 1 to get the total of 5 numbers
please input 2 to get the average of 5 numbers
please input x to exit
'''
choice=raw_input()
if choice=='1':
sum()
elif choice=='2':
average()
elif choice=='x':
pass
本程序十分基础,在此记录在运行过程的一些想法
首先,由于input输入数值则保存值也为数值类型,而raw_input()是字符型,导出时还需要转换,故在取数时用input较好。
其次print"please input number%d"%(i+1) a=input()和a=(input('please enter a number%d:'%(i+1))) 效果一样
最后,运行时,代码会先运行
print'''
please input 1 to get the total of 5 numbers
please input 2 to get the average of 5 numbers
please input x to exit
'''
再根据相应的选择运行对应的函数
(只是记录自己的学习过程)
题目如下:写一个带文本菜单的程序,菜单项如下:(1)取五个数的和;(2)取5个数的平均值 (x)退出。由用户做一个选择,然后执行相应的功能。
def sum():
total=0
for i in range(5):
print"please input number%d"%(i+1)
a=input()
total=total+a
print"total=",total
def average():
total=0
for i in range(5):
b=float(input('please enter a number%d:'%(i+1)))
total=total+b
print'the average is',total/5
print'''
please input 1 to get the total of 5 numbers
please input 2 to get the average of 5 numbers
please input x to exit
'''
choice=raw_input()
if choice=='1':
sum()
elif choice=='2':
average()
elif choice=='x':
pass
本程序十分基础,在此记录在运行过程的一些想法
首先,由于input输入数值则保存值也为数值类型,而raw_input()是字符型,导出时还需要转换,故在取数时用input较好。
其次print"please input number%d"%(i+1) a=input()和a=(input('please enter a number%d:'%(i+1))) 效果一样
最后,运行时,代码会先运行
print'''
please input 1 to get the total of 5 numbers
please input 2 to get the average of 5 numbers
please input x to exit
'''
再根据相应的选择运行对应的函数
(只是记录自己的学习过程)
相关文章推荐
- python 2-7 如何实现用户的历史记录功能(最多n条)collections.deque/pickle持久存储
- Python高级编程-如何实现用户的历史记录功能?
- 根据用户的选择输出不同的结果--函数指针的使用
- python-5-如何实现用户的历史记录功能(最多n条)?
- ASP.NET在后台代码实现个功能,根据选择提示用户是否继续执行操作
- html <select> <option> 实现根据选择不同的option,调用不同的方法功能
- ASP.NET提示框,且根据用户选择执行不同代码的实现
- 如何用vsftpd实现用户不同权限:只能下载,可上传,管理权限等
- 抱歉,没分了!!!请教linux下,用C/C++编程,如何知道我要实现某一功能需要用到什么函数
- 在.NET下如何用WebService实现身份认证,及如何跟踪用户的访问,如类似Possport的功能,不会还是用Session吧?
- vf表单中如何根据组合框内容而选择不同文件再根据不同文件改变文本框的内容
- table+iframe+变量url实现左边treeview,右边根据节点选择不同加载不同页面
- expdp/impdp如何实现不同用户之间的导出导入
- 利用BIND的VIEW功能实现国内国处用户解析到不同服务器的智能解析
- 一个类如何实现两个接口中同名同参数不同返回值的函数
- 根据用户喜欢的爱好选择不同风格CSS(ViewState)
- 根据其他页面传递不同参数实现功能与效果
- 如何实现ie禁止返回功能和禁止用户用back space键返回页面(未测试)
- sd--如何控制定价条件根据用户不同而操作不同(有的可以输入有的不可以)
- table+webusercontrol实现左边treeview,右边根据节点选择不同加载不同页面