whileループを作成しようとすると、一連の質問が表示され、最後の質問が表示されたときに再起動します。問題は、特定の単語を任意の質問に入力することによってユーザーが終了することができるようにすることです。私はループ内で可能な限り小さなコードを望んでいるので、if/elifステートメントは関数の中にあります。関数からwhileループを中断または継続する
私の質問は次のとおりです:機能からループを継続するか、中断することはできますか?
マイコード:
def check_gender(q):
if q == "f":
number_women = number_women + 1
elif q == "m":
number_men = number_men + 1
elif q == "foo":
#break the loop
else:
print("Please answer M or F: ")
q = input("Are you a male or female (M/F)? ").lower()
check_gender(q)
def check_age(q):
if not(16 <= int(q) <= 25):
print("You are not in the age range for this survey")
#Jump back to first question here
if q == "foo":
#break the loop
while True:
gender = input("Are you a male or female (M/F)? ").lower()
check_gender(gender)
age = input("Please enter your age: ")
check_age(age)
#And so on with questions
これは可能ですか?
コードが正しくインデントされていません。それを修正できますか? –