返回时出现语法错误

时间:2015-03-12 14:25:48

标签: python return syntax-error

以下代码我试图在第10行返回语法错误"返回"。我也尝试删除该部分(使代码当然不起作用)并且它很快就在打印时返回错误,因此我很确定那里存在一个问题,导致整个崩溃。想法?

def factorial(number):
    result = 1
    while number > 0:
        result = result * number
        number = number - 1
    return result

def combination(n, k):
    result = factorial(n) / (factorial(k)*factorial(n-k)
    return result

print "How many cards you want to calculate the probabilities for? (1, 2, 3)"
answer = raw_input("")
print "How many cards are in your deck?"
m = raw_input("")
print "How many cards will you draw?"
k = raw_input("")

if answer == 1:
    print "How many copies of card A do you have?"
    a = raw_input("")
    Com1 = combination(m-a, k)/float(m, k)
    print Com1

elif answer == 2:
    print "How many copies of card A do you have?"
    a = raw_input("")
    print "How many copies of card B do you have?"
    b = raw_input("")
    ### A+B-AB
    Com2 = (combination(m-a, k)+combination(m-b, k)-combination(m-a-b, k)+combination(m, k))/float(combination(m, k)
    print Com2 

elif answer == 3:
    print "How many copies of card A do you have?"
    a = raw_input("")
    print "How many copies of card B do you have?"
    b = raw_input("")
    print "How many copies of card C do you have?"
    c = raw_input("")
    ### A+B+C-A^B-A^C-B^C-ABC=A+B+C-ABC-A-B+AB-A-C+AC-B-C+BC=AB+AC+BC-A-B-C-ABC
    ### A+B+C+ABC-AB-AC-BC
    Com3 = (combination(m-a, k)+combination(m-b, k)+combination(m-c, k)+combination(m-a-b-c, k)-combination(m-a-b, k)-combination(m-a-c, k)-combination(m-b-c, k)+combination(m, k))/float(combination(m, k)
    print Com3

1 个答案:

答案 0 :(得分:2)

您在前一行缺少右括号:

result = factorial(n) / (factorial(k)*factorial(n-k)
#                       ^         ^ ^          ^   ^?
#                       1         2 2          2   21