指数函数返回问题-python
我正在研究这个问题-指数函数返回问题-python,python,exponent,Python,Exponent,我正在研究这个问题- 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
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”:
教授的程序会生成一个随机数,输入到函数中。所以你不能用“输入”来输入你自己的号码。您必须使用值“exp”。教授的代码生成一个随机数并将其放入函数中。为了检查函数是否正确运行,只需输入生成的随机数 此外,您似乎还没有在您提供的内容中初始化分数:
q2s = 0
e = random.randint(3,11)
score=0
我明白你的意思。但是,输入函数允许我接收用户输入…而“x=exp”(“请输入一个2的幂:”)不允许。您不允许进行用户输入。您教授的程序通过exp参数指定输入
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()
q2s = 0
e = random.randint(3,11)
score=0