简体   繁体   English

关于将变量传递给参数

[英]Regarding passing variables to an argument

I am working in python 2.7.8. 我正在使用python 2.7.8。

I'm currently learning about parameters and methods. 我目前正在学习参数和方法。 What I'm trying to accomplish is have a user enter two different variables then pass them to an argument within different methods, sum() and difference(). 我要完成的工作是让用户输入两个不同的变量,然后将它们传递给不同方法sum()和Difference()中的参数。

My following code is something like this: 我的以下代码是这样的:

def computeSum(x, t):
    x = int(raw_input('Please enter an integer: '))
    t = int(raw_input('Please enter a second integer: '))
    x+t
return Sum

def computeDif(y, j):
    y = int(raw_input('Please enter an integer: '))
    j = int(raw_input('Please enter a second integer: '))
    y+j
return Dif

def main():
    raw_input('Would you like to find the sum of two numbers or the difference of two numbers?: ')
    answer = 'sum'
while True:
    computeSum()
else:
    computeDif()

For some reason my compiler (pyScriptor) isn't running and I cannot see any output nor error messages, its just blank. 由于某种原因,我的编译器(pyScriptor)无法运行,并且我看不到任何输出或错误消息,只是空白。 Can anyone possibly help me with any syntax/logic errors? 有人可以在语法/逻辑错误方面为我提供帮助吗?

There are a few problems with your code 您的代码有一些问题

  • Your indentation is way off 您的缩进距离很远

  • computeSum and computeDif expect the two numbers as parameters, but then also ask for them from the terminal computeSumcomputeDif期望computeDif两个数字用作参数,然后还要从终端请求它们

  • You return the variables Sum and Dif , but never assign values to them 您返回变量SumDif ,但永远不要给它们赋值

  • You call either computeSum or computeDif , but never do anything with the returned value 你打电话或者computeSumcomputeDif ,但从来没有返回值做任何事

  • You never call main . 您永远不会称呼main Do you know that you don't need a main function? 您知道您不需要main功能吗? You can just put the code in line after the function definitions 您可以将代码放在函数定义之后

This is probably a little closer to what you had in mind 这可能更接近您的想法

def computeSum(x, t):
    return x + t

def computeDif(y, j):
    return y - j

def main():
    while True:
        answer = raw_input('Would you like to find the "sum" of two numbers or the "dif"ference of two numbers? ')
        a = int(raw_input('Please enter an integer: '))
        b = int(raw_input('Please enter a second integer: '))

        if answer == 'sum':
            print(computeSum(a, b))
        elif answer == 'dif':
            print(computeDif(a, b))
        else:
            print('Please enter "sum" or "dif"')

main()

The problem is that you don't need a main() function. 问题在于您不需要main()函数。 Just put the code, unindented, by itself, and it will run when you run the program. 只需将代码本身缩进即可,当您运行程序时它将运行。

暂无
暂无

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

相关问题 传递包含空格的shell变量作为参数 - Passing shell variables containing whitespace as argument Robot Framework-将列表变量作为关键字参数传递 - Robot Framework - Passing List variables as keyword argument 关于python中的变量参数 - regarding variable argument in python Django将变量传递给模块 - login()得到了一个意外的关键字参数 - Django passing variables to modules - login() got an unexpected keyword argument 关于传递错误的 object - regarding passing the wrong object 在从 Python 中的 statsmodels 传递到 SARIMAX() 的 exog 参数之前,我们是否需要对外生变量进行差分? - Do we need to do differencing of exogenous variables before passing to exog argument of SARIMAX() from statsmodels in Python? 当将局部变量作为参数传递时,Dask 数据帧应用给出了意外的结果 - Dask dataframe apply giving unexpected results when passing local variables as argument Python:使用在主模块中建立的变量和内容,而不将其作为函数定义中的显式参数传递 - Python: Using variables and content established in the main module without passing them as an explicit argument in the function definition 在传递一个对象作为参数时,如何在使用“yield”时更新其变量? - while passing an object as argument, how to update its variables when using “yield”? 访问 ansible 剧本中的 python class 变量而不作为命令行参数传递或存储在文件/保险库文件中 - Accessing python class variables in an ansible playbook without passing as an command line argument or storing in a file/vault file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM