只有在输入 "yes" 时,我的 while 循环才能确保给出有效答案
My while loop to make sure a valid answer is being given only goes through when "yes" is inputted
userans = input("Do you want to become a member of Friends of Seaview Pier? ").lower()
while not (userans == "yes" and "no"):
userans = input("Do you want to become a member of Friends of Seaview Pier? ").lower()
print (userans)
if userans == "yes":
Appdata = Application()
userans2 = input("Do you want to volunteer at Seaview Pier? ").lower()
if userans2 == "yes":
Vdata = Volunteerdata()
CombinedData = [Appdata, Vdata]
Data.append(CombinedData)
print(Data)
else:
print("Thank you for considering.")
Vdata = (["no"])
CombinedData = [Appdata, Vdata]
Data.append(CombinedData)
print(Data)
elif userans == "no":
print("Thank you for your consideration.")
那里有一些打印功能,所以我可以检查它在运行时输入和输出的内容。为了方便起见,我也没有包含我创建的函数,因为我很确定这是这里这段代码的问题,而不是它们。
输出和输入的示例如下:
Do you want to become a member of Friends of Seaview Pier? no
Do you want to become a member of Friends of Seaview Pier? no
no
Do you want to become a member of Friends of Seaview Pier? on
on
Do you want to become a member of Friends of Seaview Pier? no
no
Do you want to become a member of Friends of Seaview Pier? yes
yes
Enter your first name:
while not (userans == "yes" and "no")
这是检查多个值的错误方法。
改用这个:
while userans != "yes" and userans != "no":
甚至更好:
while userans not in ["yes", "no"]:
userans = input("Do you want to become a member of Friends of Seaview Pier? ").lower()
while not (userans == "yes" and "no"):
userans = input("Do you want to become a member of Friends of Seaview Pier? ").lower()
print (userans)
if userans == "yes":
Appdata = Application()
userans2 = input("Do you want to volunteer at Seaview Pier? ").lower()
if userans2 == "yes":
Vdata = Volunteerdata()
CombinedData = [Appdata, Vdata]
Data.append(CombinedData)
print(Data)
else:
print("Thank you for considering.")
Vdata = (["no"])
CombinedData = [Appdata, Vdata]
Data.append(CombinedData)
print(Data)
elif userans == "no":
print("Thank you for your consideration.")
那里有一些打印功能,所以我可以检查它在运行时输入和输出的内容。为了方便起见,我也没有包含我创建的函数,因为我很确定这是这里这段代码的问题,而不是它们。
输出和输入的示例如下:
Do you want to become a member of Friends of Seaview Pier? no
Do you want to become a member of Friends of Seaview Pier? no
no
Do you want to become a member of Friends of Seaview Pier? on
on
Do you want to become a member of Friends of Seaview Pier? no
no
Do you want to become a member of Friends of Seaview Pier? yes
yes
Enter your first name:
while not (userans == "yes" and "no")
这是检查多个值的错误方法。
改用这个:
while userans != "yes" and userans != "no":
甚至更好:
while userans not in ["yes", "no"]: