我是python的新手,正在编写以下代码(用于查找平均值的程序)
sum=0
for i in range(1,9999):
n=int(input("Enter your number: "))
i+=1
sum=n+sum
a=input("Do you want to add more (Y/N): ")
if a=="Y" or a=="y":
continue
elif a=="N" or a=="n":
break
else:
print("Error: Incorrect choice")
i-=1
avg=sum/i
print(avg)一切都很好,但是如果用户输入了“不正确的选择”,我如何让它跳转到"if语句“,它再次询问用户是否要添加更多的数字?在结构化编程中,我认为可以使用"goto“或"label”,但python是面向对象的,我如何才能让它在这里工作。敬请指教。
发布于 2019-12-28 05:36:53
有几种方法可以使用"while“
while a not in ["Y","y","N","n"]:
a=input("Do you want to add more (Y/N): ")https://stackoverflow.com/questions/59506196
复制相似问题