假设用python写一个指数,无法弄清楚我错了什么



我应该计算X到Y并输出最终结果。

 for i in range (X):
   X, Y = Y, X
 print (X)

这就是我得到的

程序输入失败:1 2
预期输出: 1
您的程序输出:2

程序有什么问题?

以下是您当前正在做的事情:

for i in range (X):
  # for the first input number (1), swap X and Y
  X, Y = Y, X
# because X is 1 and Y is 2, and you swapped them once, X is now 2 and Y is 1
print (X)

这是你应该做的:

Ans = 1 # store the new answer
for i in range(Y):
  Ans = Ans*X # multiply the answer by X (doing this Y times)
# print the answer
print(Ans)

相关内容

最新更新