简体   繁体   English

在 for 循环中调用函数 - python

[英]calling functions in for loop - python

i have a lab for my computer science class and i can't seem to get it right.我有一个用于我的计算机科学 class 的实验室,但我似乎做错了。 we're required to import everything from math and to define functions for geometric shapes我们需要从数学中导入所有内容并为几何形状定义函数

ex:前任:

    return b * h / 2 

for calling it, we need to make a menu to make the user choose what they want for these geometric shapes.为了调用它,我们需要制作一个菜单,让用户为这些几何形状选择他们想要的东西。 for example, if you chose the number 2 and it gives you the volume of a cylinder.例如,如果您选择数字 2,它会为您提供圆柱体的体积。

i just can't seem to understand how you call the function inside the while/for loops.我似乎无法理解您如何在 while/for 循环中调用 function。

thank uu~!谢谢你~!

def find_area(width, height): # define function
    return width * height

for i in range(10):
    area = find_area(i, 10) #calls function 'find_area()' and saves returned 
                            #value to variable area
    print(area) # prints area
count = 0
while count < 10:
    area = find_area(i, 10) #calls function 'find_area()' and save returned 
                            #value to variable area
    print(area)
    count += 1

Example calling function find_area() from within a for-loop and while-loop.从 for 循环和 while 循环中调用 function find_area()的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM