首页 > 解决方案 > 如何在 Python 3 中获取结果后重复此代码

问题描述

我编写了一个 python 代码来查找指数值。现在我想在得到一个数字的结果后自动重新运行这段代码,这意味着无限次询问另一个数字

# Function to Find Exponential Value of any Number
def expo(num, exp):
    result = num ** exp
    return result

# Taking Input
Number = int(input("Please Enter Desired Number: "))
Exponent = int(input("Please Provide Power Value: "))

# Passing to Function
ExpoVal = expo(Number, Exponent)

# String Section
str1 = "Exponential Value of %s" % Number
str2 = " with Power of %s" % Exponent
str3 = " is :"
str_concatenate = str1 + str2 + str3

# Output Result
print(str_concatenate, ExpoVal)

标签: pythonpython-3.x

解决方案


您可以将整个代码添加为单个函数,然后通过while True:循环运行它。这使它无限运行:

def Expo():
    Number = int(input("Please Enter Desired Number: "))
    Exponent = int(input("Please Provide Power Value: "))

    result = Number ** Exponent

    str1 = "Exponential Value of %s" % Number
    str2 = " with Power of %s" % Exponent
    str3 = " is :"
    str_concatenate = str1 + str2 + str3

    print(str_concatenate, ExpoVal)

while True:
    Expo()

推荐阅读