简体   繁体   English

调用函数

[英]Calling functions

this may sound proper nooby, but I'm having some trouble with python functions.这可能听起来很笨拙,但我在使用 python 函数时遇到了一些麻烦。

i do a level computer science and I'm making a currency converter using a dictionary to define the exchange rates of the currencies.我做了一个水平的计算机科学,我正在使用字典制作货币转换器来定义货币的汇率。

for each conversion, i have made a function that does all the calculations etc for the converting.对于每次转换,我都创建了一个函数,可以为转换进行所有计算等。 for example, if i wanted to convert sterling to euro, my script would use the SE function.例如,如果我想将英镑转换为欧元,我的脚本将使用 SE 函数。 after i have defined all the functions, there is a algorithm that says在我定义了所有函数之后,有一个算法说

if (i == 'SE'):
    return(SE)

because SE is the function name for this conversion.因为 SE 是此转换的函数名称。 however, when i run this code, it tells me that return cannot be used outside of a function, so is there a way in calling up the function?但是,当我运行此代码时,它告诉我不能在函数之外使用return ,那么有没有办法调用该函数?

my code without using functions can be found here: http://pastebin.com/bt23MeTn我不使用函数的代码可以在这里找到: http : //pastebin.com/bt23MeTn

my code with the function attempt can be found here: http://pastebin.com/sLFwrDT8我的函数尝试代码可以在这里找到: http : //pastebin.com/sLFwrDT8

for those of you who are wondering why I'm using a dictionary, its because my teacher said that the exchange rates can change anytime so using a dictionary will mean the user only has to change the dict entry instead of changing each individual formula.对于那些想知道我为什么使用字典的人来说,这是因为我的老师说汇率可以随时变化,所以使用字典意味着用户只需要更改字典条目而不是更改每个单独的公式。 i suppose i could make a scraper algorithm to scrape a website for each rate, but that'll probably be in my next version :)我想我可以制作一个刮刀算法来为每个费率刮一个网站,但这可能会出现在我的下一个版本中:)

return is used to return variables from a function definition . return用于从函数定义中返回变量。

To call a function you have simply to write the name of the function with the input parameters in the round brackets.要调用一个函数,您只需在圆括号中写下带有输入参数的函数名称。 In your case SE function has not input parameters so you have to call it as follow:在您的情况下, SE函数没有输入参数,因此您必须按如下方式调用它:

if i == 'SE':
    SE()

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

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