写一个 Python 程序,用一个例子求出循环数、While 循环数和幂函数的幂。
这个 Python 程序允许用户输入任何数值,指数。接下来,这个 Python 程序使用求循环来求一个数的幂。
# Python Program to find Power of a Number
number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = 1
for i in range(1, exponent + 1):
power = power * number
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
Python 数字幂用于循环输出
Please Enter any Positive Integer : 3
Please Enter Exponent Value : 4
The Result of 3 Power 4 = 81
这个数字程序的幂和上面一样,但是这次我们用的是 While Loop
# Python Program to find Power of a Number
number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = 1
i = 1
while(i <= exponent):
power = power * number
i = i + 1
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
使用 while 循环输出 时的 Python 幂
Please Enter any Positive Integer : 10
Please Enter Exponent Value : 5
The Result of 10 Power 5 = 100000
这个 Python 程序示例接受来自用户的数字和指数。接下来,我们使用内置的数学函数调用幂来求一个数的幂。
# Python Program to find Power of a Number
import math
number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = math.pow(number, exponent)
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
原创文章,作者:FQF70,如若转载,请注明出处:https://www.506064.com/n/127142.html