[英]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 computeSum
和computeDif
期望computeDif
两个数字用作参数,然后还要从终端请求它们
You return the variables Sum
and Dif
, but never assign values to them 您返回变量Sum
和Dif
,但永远不要给它们赋值
You call either computeSum
or computeDif
, but never do anything with the returned value 你打电话或者computeSum
或computeDif
,但从来没有返回值做任何事
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.