各入力をテストしようとしていますが、数値がクリアされてから数式が実行されます。例えば、ユーザは数字ではなくNを入力し、数字ではなく数字を出力したいのに対し、ユーザが1を入力した場合は、次の関数に移動して同じことをしたい問題の答えを出力する最後のセクションに進みます。Python Recrusive Statementエラーが定義されていません
プログラムは、非数値領域の両方のエラーをまだ通過していますが、最後に機能が得られたときには、基数や電力が定義されていることを伝えています。
コードはPython2とPython3で書かれています。すべてがうまく動作します。私はほとんどpython3を使用します。
[テストここでPicture/Error Msg][1]
# Below we are creating the recursive statement to do the math for us. We are calling Base and Power
# from the main function where the user Inputs the numbers.
def pow(base, power):
if power == 0:
return 1
if power == 1:
return base
else :
return base * pow(base, power - 1)
def determineBase():
while True:
try:
base = int(input ('Please Enter A Base: '))
except ValueError:
print("Please use whole numbers only. Not text nor decimals.")
continue
else:
return base
def determinePower():
while True:
try:
power = int(input ('Please Enter A Power: '))
except ValueError:
print("Please use whole numbers only. Not text nor decimals.")
continue
else:
return power
def main():
determineBase()
determinePower()
pow(base,power)
print("The answer to",base,"to the power of", power,"is", pow(base,power),".")
main()
再帰的リターンと次のdefの間にギャップを入れてみてください。スクリーンショットからそれらのラインを削除してください。スタックトレース全体を見ることができますか? –
@James_Hughesは、スペースが何も変更しなかったとは限りません。私はあなたのために別の写真を追加しました。 48行目と46行目でエラーが発生しています – Yrroth
問題は再帰関数自体にはありません。下記を参照してください。 –