我想在Python中重复必要的if语句

问题描述

elif opt==2:
    nitem=str(input('enter the porduct name'))
    nitemc=int(input('enter the item code'))
    nstkq=int(input('enter stock qty'))
    nitemtyp=str(input('enter item type'))
    b={'itemname':nitem,'itemcode':nitemc,'item-type':nitemtyp,'stock':nstkq}
    stock=stock.append(b,ignore_index=True)
    x=str(input('do you want to add another one? \n "y" for yes "n" for no' ))
    if x=='y':
        <to repeat>

我应该使用什么代码来使代码再次重复用户请求的时间?

希望我会得到帮助,谢谢!!!!!!!

解决方法

elif opt==2:
    while True:
        nitem=str(input('enter the porduct name'))
        nitemc=int(input('enter the item code'))
        nstkq=int(input('enter stock qty'))
        nitemtyp=str(input('enter item type'))
        b={'itemname':nitem,'itemcode':nitemc,'item-type':nitemtyp,'stock':nstkq}
        stock=stock.append(b,ignore_index=True)
        x=str(input('do you want to add another one? \n "y" for yes "n" for no' ))
        if x=='n':
            break