我是一个新的python程序员,目前正在用python开发一个非常简单的转换器。我现在有这个,但我想让它,这样'n'可以随时改变。在
#inches to centimeters
def in_inches(n):
resulti = n * 100
return resulti
n = 10
resulti = in_inches(n)
print "In %d inches we have %d centimeters" % (n, resulti)
#pounds to kilograms
def in_pounds(x):
resultp = x * 0.453592
return resultp
x = 10.0
resultp = in_pounds(x)
print "In %d pounds we have %d kilograms." % (x, resultp)
您可以创建单个函数并在其中执行所有转换,而不是生成许多函数:
输出:
^{pr2}$您可以获得注释中提到的
raw_input()
(Py3为input()
),也可以将它们作为脚本的参数。下面是一个小例子,它只收集所有的-i
参数来表示in_inches()
,所有的-p
参数表示in_pounds()
:然后,您只需使用您想要的任何参数调用脚本:
^{pr2}$我想这就是你想要的。在
这个程序有一个菜单,询问要进行哪种转换,然后根据这个选择从用户那里获得一个数字输入,然后输出正确的转换。在
相关问题 更多 >
编程相关推荐