【问题标题】:Printing integers of list so that the program asks after every number if you want to print the next打印列表的整数,以便程序在每个数字之后询问您是否要打印下一个
【发布时间】:2022-11-01 20:05:17
【问题描述】:

“打印列表 [2, 6, 4, 3, 7] 的整数,以便程序在每个数字后询问“您要打印下一个数字(y/n)吗?”当用户输入 n 时程序终止.

我刚刚开始使用python,但我无法弄清楚这一点。我现在完全迷路了,甚至不知道如何开始,希望有人可以提供帮助

列表 = 2、6、4、3、7 n = int(input('你想打印下一个数字 y/n 吗?'))

从这里我被卡住了,我不知道如何前进

【问题讨论】:

    标签: list loops


    【解决方案1】:

    这很简单,一种方法是循环列表,同时包裹在一个无限循环中,然后在用户点击n时爆发

    例如:

    while True:
        for item in [2, 6, 4, 3, 7]:
            print(item)
            continue = input('do you want to print the next number? y/n')
            if continue.lower() == 'n':
                print('you opted to exit')
                break
            else:
                # just continue the loop
                # to do this you can remove else, add pass, or use continue
                continue
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-18
      • 1970-01-01
      • 2012-03-26
      • 2021-02-02
      • 2023-02-01
      相关资源
      最近更新 更多