在用户输入后添加到输入提示

Adding onto an input prompt after the user inputs

我正在尝试制作 "pi-practicing' program in python, and I want the user's input, if correct, to be placed next to the "3."。

我有:

numbers = [1,4,1,5,9,2,6,5]

def sequence():
    i = input("3.") 
    y = int(i)
    if y == numbers[0]:
        print ("Good job!")
        #??????
        numbers.pop(0)
        sequence()
    else:
        print("nope")
        sequence()
sequence()

所以当提示时,如果用户输入1作为第一个数字,我希望下一个输入提示是3.1,所以用户必须输入4,依此类推。

提前致谢! -rt

您不需要递归一个简单的 while 循环即可。利用全局变量通常不是好的做法:

def sequence():
    numbers = [1,4,1,5,9,2,6,5]
    prompt = '3.'
    while numbers:
        i = input(prompt)
        y = int(i)
        if y == numbers[0]:
            print ("Good job!")
            prompt += i
            numbers.pop(0)
        else:
            print("nope")
sequence()