繁体   English   中英

在python中,如何从用户那里获取输入并在输入后跳转到不同的行?

[英]In python how can I take input from user and jump to a different line after taking input?

我是 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 是面向对象的,我如何让它在这里工作。 请指教。

有几种方法可以使用“while”

while a not in ["Y","y","N","n"]:
    a=input("Do you want to add more (Y/N): ")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM