for循环到while循环

时间:2017-02-25 12:55:13

标签: python python-3.x loops for-loop while-loop

我正在尝试将其更改为while循环。

number = int(input("enter number : "))
exponent = int(input("enter exponent : "))
result = number

for i in range(1, exponent):
    result = number * result
print(number, " to the power of ", exponent, " is ", result)

2 个答案:

答案 0 :(得分:1)

您有4种可能性:

number = int(input("enter number : "))
exponent = int(input("enter exponent : "))

# 1st possibility
result = number
for i in range(1, exponent):
    result = number * result
print(number, " to the power of ", exponent, " is ", result)


# 2nd possibility
result = number
i = 1
while i < exponent:
    result = number * result
    i += 1

print(number, " to the power of ", exponent, " is ", result)

# 3rd possibility
print(number, " to the power of ", exponent, " is ", number**exponent)

# 4th possibility
print(number, " to the power of ", exponent, " is ", pow(number, exponent))

你应该选择#3或#4;)。

答案 1 :(得分:0)

number = int(input("enter number : "))
exponent = int(input("enter exponent : "))
result = number
i = 1
while i < exponent:
    result = number * result
    i += 1
print(number, " to the power of ", exponent, " is ", result)

那是你在找什么?