私はPythonの初心者です。ユーザーが無効な入力を行うまで関数を呼び出す方法が不思議でした。ここ はコードのサンプルです:無効な入力まで関数を呼び出す方法は?
start = input("For sum of squares, type 'squares'. For sum of cubes, type 'cubes'. "
"\nIf you would like to raise a number to something other than 'squares' or 'cubes', type 'power'. "
"\nIf you would like to exit, type 'exit':")
def main_function(start):
while start.lower() != "exit":
if start.lower() in "squares":
initial = input("What is the initial constant for the sum of the squares: ")
terms = input("Number of terms: ")
if start.lower() in "cubes":
initial = input("What is the initial constant for the the sum of the cubes: ")
terms = input("Number of terms: ")
if start.lower() in "power":
initial = input("What is the initial constant for the the sum: ")
terms = input("Number of terms: ")
else:
print("Program halted normally.")
quit()
main_function(start)
私はそれが何を取得しようとしています何を求めるプロンプトは表示ユーザーが適切な入力を入力した場合「開始」し、それが再び機能を介して実行するために取得することです。私は 'else'ステートメントの上と下の関数内に 'start'を入れようとしましたが、新しい入力を受け付けません。
ループインループを使用するとユーザーの入力に最も適しています – dawg
whileループ内で別のwhileループを追加しない限り、私が使っていることは何ですか? – Veyronvenom1200
@ user7433120ビンゴ:) – Peter