Python 如何从一个函数到另一个函数获取变量? def getInt(): 尽管如此: 宽度=整数(输入(“输入宽度(1-60):”) 如果宽度大于1且宽度小于60: 打破 其他: 打印(“请输入有效的输入”) 尽管如此: 高度=整数(输入(“输入宽度(1-20):”) 如果高度>1且高度1且高度1且高度
当我跑步时,它说: 回溯(最近一次呼叫最后一次): main() 打印('周长为{}'。格式(CalcPrimeter(宽度、高度))) NameError:未定义名称“宽度”Python 如何从一个函数到另一个函数获取变量? def getInt(): 尽管如此: 宽度=整数(输入(“输入宽度(1-60):”) 如果宽度大于1且宽度小于60: 打破 其他: 打印(“请输入有效的输入”) 尽管如此: 高度=整数(输入(“输入宽度(1-20):”) 如果高度>1且高度1且高度1且高度,python,python-3.x,function,Python,Python 3.x,Function,当我跑步时,它说: 回溯(最近一次呼叫最后一次): main() 打印('周长为{}'。格式(CalcPrimeter(宽度、高度))) NameError:未定义名称“宽度” 也许你可以试着把main()改成这个 def getInt(): while True: width = int(input("Enter width (1 - 60):")) if width > 1 and width < 60: break
也许你可以试着把main()改成这个
def getInt():
while True:
width = int(input("Enter width (1 - 60):"))
if width > 1 and width < 60:
break
else:
print("Please enter a valid input")
while True:
height = int(input("Enter width (1 - 20):"))
if height > 1 and height < 20:
break
else:
print("Please enter a valid input")
return width, height
def calcPerimeter(width, height):
perimeter = (2 * (width + height))
return perimeter
def calcArea(width, height):
area = width * height
return area
def main():
print('Results: {}'.format(getInt()))
print('The perimeter is {}' .format(calcPerimeter(width, height)))
print('The area is {}' .format(calcArea(width, height)))
main()
在传递到另一个函数之前,需要将getInt()返回到特定变量
另一种方法是在getInt()函数内调用calculatePerimeter和calculateArea,以下是示例
def main():
width, height = getInt()
print('Results: {}, {}'.format(width, height))
print('The perimeter is {}' .format(calcPerimeter(width, height)))
print('The area is {}' .format(calcArea(width, height)))
def getInt():
尽管如此:
宽度=整数(输入(“输入宽度(1-60):”)
如果宽度大于1且宽度小于60:
打破
其他:
打印(“请输入有效的输入”)
尽管如此:
高度=整数(输入(“输入宽度(1-20):”)
如果高度>1且高度<20:
打破
其他:
打印(“请输入有效的输入”)
返回测径仪(宽度、高度)、测径仪(宽度、高度)
def测厚仪(宽度、高度):
周长=(2*(宽度+高度))
返回周长
def Calcrea(宽度、高度):
面积=宽度*高度
返回区
def main():
周长,面积=getInt()
打印('周长为{},面积为{}'。格式(周长,面积))
main()
一种方法是使用全局关键字。另一种方法是从函数中返回所需的变量,并将它们保存在全局范围中。您应该阅读这篇文章,并大致学习变量范围的规则。作为一名程序员,这是必不可少的知识
参考此(链接)[了解为什么将main
函数更改为Po的答案会使代码正确运行
函数calcPerimeter
和calcrea
中定义的变量对于main
函数不可见/不可访问,因为它们位于不同的作用域中。#我想我已经解决了您的问题。在您的例子中,它返回列表/元组。您必须将其转换为简单变量
def getInt():
while True:
width = int(input("Enter width (1 - 60):"))
if width > 1 and width < 60:
break
else:
print("Please enter a valid input")
while True:
height = int(input("Enter width (1 - 20):"))
if height > 1 and height < 20:
break
else:
print("Please enter a valid input")
return calcPerimeter(width, height), calcArea(width, height)
def calcPerimeter(width, height):
perimeter = (2 * (width + height))
return perimeter
def calcArea(width, height):
area = width * height
return area
def main():
perimeter, area = getInt()
print('Perimeter is {}, area is {}'.format(perimeter, area))
main()
def getInt():
尽管如此:
宽度=整数(输入(“输入宽度(1-60):”)
如果宽度大于1且宽度小于60:
打破
其他:
打印(“请输入有效的输入”)
尽管如此:
高度=整数(输入(“输入高度(1-20):”)
如果高度>1且高度<20:
打破
其他:
打印(“请输入有效的输入”)
返回宽度、高度
def测厚仪(宽度、高度):
hw=宽度+高度
周长=2*hw
返回周长
def main():
宽度,高度=getInt()
p=测厚仪(宽度、高度)
打印('周长为%s“%p”)
a=石灰面积(宽度、高度)
打印('区域为%s'%a)
main()
你知道变量作用域吗
?你认为width
在main
方法中可见吗?getInt()
返回宽度和高度,但是当你调用getInt()
时,你不会将返回的值保存在任何地方。试试这个:width,height=getInt()
。如果我在getInt中打印这些值,我会将它们作为值返回,但我无法将它们输入到其他函数中。
#I think i have resolved your problem.In your case it was returning list/tuple .you have to convert it into simple variable
def getInt():
while True:
width = int(input("Enter width (1 - 60):"))
if width > 1 and width < 60:
break
else:
print("Please enter a valid input")
while True:
height = int(input("Enter height (1 - 20):"))
if height > 1 and height < 20:
break
else:
print("Please enter a valid input")
return width,height
def calcPerimeter(width, height):
hw=width+height
perimeter = 2*hw
return perimeter
def main():
width,height= getInt()
p=calcPerimeter(width, height)
print('The perimeter is %s'%p)
a=calcArea(width, height)
print('The area is %s'%a)
main()