如何让代码从特定行重新运行? | python | python 技术论坛-大发黄金版app下载

我做了一个程序,类似这样:

print("hello")
test="123"
list=["1","2","3"]
aaa=input("输入a运行程序a,输入b运行程序b")
if aaa=="a":
    print("hello,a")
elif aaa=="b":
    print("hello,b")
else:
    print("no")
print("goodbye")

假如我输入了”a”,并且运行完了第11行(最后一行)的代码,请问怎么样让代码重新运行第三行,让我继续输入”b”?(需要保存列表和变量内容)

jason990420
最佳答案

while 语句 - 用于在表达式保持为真的情况下重复地执行.

for 语句 - 用于对序列(例如字符串、元组或列表)或其他可迭代对象中的元素进行迭代.

前者通常用在不定次数重复执行, 后者则用在有限次数重复执行, 你的问题适用 while 语句.

方式基本上有以下两种:

def func(arg):
    print(f"hello, {arg} !")
while true:
    answer = input("输入姓名: ")
    if answer:
        func(answer)
    else:
        break
def func(arg):
    print(f"hello, {arg} !")
running = true
while running:
    answer = input("输入姓名: ")
    if answer:
        func(answer)
    else:
        running = false
2个月前
讨论数量: 1
jason990420

while 语句 - 用于在表达式保持为真的情况下重复地执行.

for 语句 - 用于对序列(例如字符串、元组或列表)或其他可迭代对象中的元素进行迭代.

前者通常用在不定次数重复执行, 后者则用在有限次数重复执行, 你的问题适用 while 语句.

方式基本上有以下两种:

def func(arg):
    print(f"hello, {arg} !")
while true:
    answer = input("输入姓名: ")
    if answer:
        func(answer)
    else:
        break
def func(arg):
    print(f"hello, {arg} !")
running = true
while running:
    answer = input("输入姓名: ")
    if answer:
        func(answer)
    else:
        running = false
2个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图