简体   繁体   English

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

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

am new to python and was working on the following code (program to find average)我是 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)

All good, but if the user inputs "Incorrect choice" how can I make it jump to the "if statement" where it asks the user again whether he/she wants to add more number?一切都很好,但是如果用户输入“不正确的选择”,我怎样才能让它跳转到“if 语句”,它会再次询问用户他/她是否想要添加更多数字? In structured programming I think "goto" or "label" can be used, but python is object oriented, how can I make it work here.在结构化编程中,我认为可以使用“goto”或“label”,但是 python 是面向对象的,我如何让它在这里工作。 Please advice.请指教。

there are several way you can use "while"有几种方法可以使用“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.

相关问题 如何防止 Spyder 在接受用户输入后插入新行? - How can I prevent Spyder from inserting a new line after taking user input? 如何在打印行上方获取用户的输入? - How can I take input from the user above a printed line? 如何从另一个应用程序获取管道输入并在之后获取用户输入? - How can I take piped input from another application AND take user input after? 如何在python中获取用户输入时跳过空白行? - How to skip blank line while taking input from user in python? 如何在 Python 中存储来自命令行的用户输入? - How can I store user input from command line in Python? 如何将输入作为图像而不是从 python 中的相机获取输入 - How to take the input as an image instead of taking input from the camera in python 如何有效地使用 input().split() 在一行中从用户那里获取至少 2 个输入? - How can i use input().split() effectively to take at least 2 inputs from user in one line? 如何从用户那里获取多个(大量)输入并在用户提供的每个输入中重复代码? (Python) - How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python) 如何仅从一行获得原始输入? - How can I take raw input from only one line? 在 PYTHON 中接受用户的输入 - Taking input from the user in PYTHON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM