[英]Exponential function return problem -python
我正在解決這個問題-
modify powerOfTwo() to meet the conditions below
# - accept 1 integer parameter; an exponent
# - print to the screen 2 raised to the exponent argument, example:
# --- "2 to the power of 2 is 4"
# - also return the result (for the example, you would return 4)
我的代碼如下:
def powerOfTwo(exp):
x = int(input("please enter a number for what power of 2: "))
e = (2**x)
print("2 to the power of",x, "is: ",e,)
return (2**x)
運行該程序時,我教授的代碼檢查我的代碼的准確性,並且我認為它檢查不正確。 程序返回以下內容,其中包括我自己輸入的“ 5”:
please enter a number for what power of 2: 5
2 to the power of 5 is: 32
+3 function call w/ correct # of params is present
+2 return value is correct type: <class 'int'>
-- return value is incorrect
Expected: 64
Returned: 32
期望值64顯然是不正確的。 我的代碼或教授的代碼在檢查我的問題是否有問題? (包括在下面):
q2s = 0
e = random.randint(3,11)
try:
print("\nq2-1: Checking powerOfTwo(" + str(e) + ")")
p2 = powerOfTwo(e)
print("+3 function call w/ correct # of params is present")
score += 3
q2s += 3
if isinstance(p2, int):
print("+2 return value is correct type:",type(p2))
score += 2
q2s += 2
if p2 == (2 ** e):
print("+2 return value is correct:",p2)
score += 2
q2s += 2
else:
print("-- return value is incorrect")
print("Expected: ",2 ** e)
print("Returned: ",p2)
e = random.randint(12,22)
print("\nq2-2: Checking powerOfTwo(" + str(e) + ")")
p2 = powerOfTwo(e)
if p2 == (2**e):
print("+2 return value is correct:",p2)
score += 2
q2s += 2
else:
print("-- return value is incorrect")
print("Expected: ",2**e)
print("Returned: ",p2)
else:
print("-- return value is incorrect type:",type(p2))
except:
print("**Something is wrong with powerOfTwo()")
printErr()
您教授的程序會生成一個隨機數字,該數字將輸入到您的函數中。 因此,您不能使用“輸入”來輸入您自己的號碼。 您必須使用值'exp'。
您教授的代碼會生成一個隨機數,並將其放入函數中。 為了檢查功能是否正常運行,您只需要輸入生成的隨機數即可。
此外,您似乎還沒有初始化所提供的分數:
q2s = 0
e = random.randint(3,11)
score=0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.