我在Ex-5学习练习中艰难地学习Python,它指出
尝试编写一些将英寸和磅转换为厘米和公斤的变量。不要只输入测量值。用Python计算数学。
到目前为止,我已经做到了:
inches = 1
centimeters = 1
convert = centimeters * 2.54
print inches
print centimeters
print "1 inch is %s centimeters." % convert
Run Code Online (Sandbox Code Playgroud)
现在将显示1英寸的转换,如何更改它以便用户输入以英寸或厘米为单位的金额,并正确显示转换?
我是否正确地认为,要成功地转换值,我将不得不手动输入值,或者是否有办法在Python中完成此操作?
这个函数不是从脚本中调用变量吗?这与Zed Shaw所说的相反......
我把评论放在自己身上,我的问题是这样的:
Zed Shaw说:
函数中的变量未连接到脚本中的变量
但是从我看到的,函数在缩进的顶部,然后当缩进开始创建变量然后链接到函数.
这个函数不是从脚本中调用变量吗?
我找不到合适的答案:有人可以详细说明这段脚本并告诉我,我是否错误地查看了它?
# This is the function with declared variables inside
def cheese_and_crackers(cheese_count, boxes_of_crackers):
print "You have %d cheeses!" % cheese_count
print "You have %d boxes of crackers!" % boxes_of_crackers
print "Man thats enough for a party!"
print "Get a Blanket. \n"
# This declares the amounts in the functions variables
print "We can just give the function numbers directly:"
cheese_and_crackers (20, 30)
# This variable sets the amounts
print "OR, we can …Run Code Online (Sandbox Code Playgroud)