python 2.7.5,使用函数处理用户输入,分配新变量和多个函数

时间:2013-11-25 17:17:31

标签: python function input output

刚刚开始使用python 2.7.5中的函数,正在为学校分配但是输出正确结果时出现问题,我希望用户输入3个数字并获得它们的产品。我可以得到2个数字的总和,但我的问题是我可以从用户那里得到输入,但输出要么显示不正确,要么我得到一个错误,这是我的代码到目前为止:(我很新编码所以请尽可能详细)

#first function for getting numbers
def getValue():
    num = float(raw_input("Enter a number: "))     
    return num     

#The second function to calculate the total
def getSum(first, second):
    total = first + second
    return total
def getProd(n1,n2,n3): #my attempt for getting the product of 3 numbers entered
    itotal = n1 * n2 * n3 

#The third function to display the data
def displayData(first, second, total):
    print("The first value is = ", first);
    print("The second value is = ", second);
    print("The Total is = ", total);

def main():
    isum1 = 0  # need to initialize the needed variables
    isum2 = 0 #how do I enter for a third number for the product? 
    isum3 = 0 
    totalSum = 0   

    isum1 = getValue()  #call the function to get a number
    isum2 = getValue()
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3) #declared itotal as var for getting the 
                                            #getProd function 

    totalSum = getSum(isum1, isum2) 

    displayData(isum1, isum2, totalSum) #unclear how to write the display correctly
main()

我的问题是我需要一个产品功能,但似乎无法使其正确。如果我放了一个isum3并将一个新函数定义为例如getProd,它就无法正确显示,

1 个答案:

答案 0 :(得分:1)

您的getProd是一个良好的开端,但您需要return才能使其有用。

def getProd(n1,n2,n3):
    itotal = n1 * n2 * n3 
    return itotal

displayData不适合显示产品数据,因为它提到“总数”并且只有两个变量。但是,您可以编写另一种具有类似行为的方法:

def displayProductData(first, second, third, total):
    print("The first value is = ", first);
    print("The second value is = ", second);
    print("The third value is = ", third)
    print("The Total is = ", total);

最后,您需要更新main,以便正确使用新方法。

def main():
    isum1 = 0
    isum2 = 0
    isum3 = 0 

    isum1 = getValue()
    isum2 = getValue()
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3)

    displayProductData(isum1, isum2, isum3, itotal)

其他杂项改进

Python style guide建议在命名变量和函数时使用lower_case_with_underscores,而不是mixedCase

def get_value():
    #code

def get_sum():
    #code

def get_prod()

在函数中,可以直接返回表达式,而不必先将其赋值给变量。

def get_sum(first, second):
    return first + second

def get_prod(n1,n2,n3):
    return n1 * n2 * n3

main中,您写道“您需要初始化所需的变量”,但实际上您必须在函数开头执行此操作!您可以根据需要分配给变量。只是不要尝试使用尚未定义的变量。

def main():
    isum1 = getValue()
    isum2 = getValue()
    isum3 = getValue() 
    itotal = getProd(isum1, isum2, isum3)

    displayProductData(isum1, isum2, isum3, itotal)

如果您使用的是Python 2而不是Python 3,那么print不需要使用括号。事实上,结果可能看起来更好,因为没有括号它不会将你的表达式解释为元组。

def display_product_data(first, second, third, total):
    print "The first value is = ", first
    print "The second value is = ", second
    print "The third value is = ", third
    print "The Total is = ", total

输出曾经是:

('The first value is = ', 2.0)
('The second value is = ', 3.0)
('The third value is = ', 4.0)
('The Total is = ', 24.0)

现在是:

The first value is =  2.0
The second value is =  3.0
The third value is =  4.0
The Total is =  24.0    

您不需要使用分号。他们只是为了让用户在使用其他语言时感到舒适。并且在一行上划分多个语句,但这并不常见。