return 上的语法错误
Syntax error on return
有以下代码,我试图让 运行 returns 第 10 行 "return" 出现语法错误。我还尝试删除该部分(当然使代码无法正常工作),但不久之后它在打印时返回了一个错误,所以我很确定那里存在导致整个崩溃的问题。想法?
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
前一行缺少右括号:
result = factorial(n) / (factorial(k)*factorial(n-k)
# ^ ^ ^ ^ ^?
# 1 2 2 2 21
有以下代码,我试图让 运行 returns 第 10 行 "return" 出现语法错误。我还尝试删除该部分(当然使代码无法正常工作),但不久之后它在打印时返回了一个错误,所以我很确定那里存在导致整个崩溃的问题。想法?
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
前一行缺少右括号:
result = factorial(n) / (factorial(k)*factorial(n-k)
# ^ ^ ^ ^ ^?
# 1 2 2 2 21