为什么我收到的是字符串中的字母而不是字符串本身?

问题描述 投票:0回答:1

#列表“物品”是[在代码末尾:“print(quantity, '*', items[item], 'Added')”] #当我输入物品编号时不给出物品。相反,它会在 #the 字符串之一中打印一个字母。请帮忙。 (在 Microsoft Visual Studio 2019 中编程)

items = ['Abc','Def','Ghi']
users = ['[email protected], examplepassword123']
user = ''

while True:
    print('')
    print('Welcome to the canteen')
    print('Options:')
    print('  Login')
    print('  Create Account')
    print('  Leave')
    cont1raw = input('Please select: ')
    cont1 = cont1raw.lower()
    if 'leave' in cont1:
        quit()
    elif 'create' in cont1:
        print('Please visit the canteen to create a new account')
    elif 'login' in cont1:
        user = input('Please enter email and password (email, password): ')
        while True:
            if user in users:
                rep1 = 1
                for items in items:
                    print(str(rep1) + ' - ' + items)
                    rep1 = rep1 +1
                item = int(input('Please enter item number: '))
                quantity = str(input('Please enter item quantity: '))
                item = item -1
                print(quantity, '*', items[item], 'Added')
python list
1个回答
0
投票

您的第二个

while
循环中存在一些问题,这是修复后的代码:

items = ['Abc','Def','Ghi']
users = ['[email protected], examplepassword123']
user = ''

while True:
    print('')
    print('Welcome to the canteen')
    print('Options:')
    print('  Login')
    print('  Create Account')
    print('  Leave')
    cont1raw = input('Please select: ')
    cont1 = cont1raw.lower()
    if 'leave' in cont1:
        quit()
    elif 'create' in cont1:
        print('Please visit the canteen to create a new account')
    elif 'login' in cont1:
        user = input('Please enter email and password (email, password): ')
        while True:
            if user in users:
                rep1 = 1
                for item in items:
                    print(str(rep1) + ' - ' + item)
                    rep1 = rep1 +1
                number = int(input('Please enter item number: '))
                quantity = str(input('Please enter item quantity: '))
                number = number -1
                print(quantity, '*', items[number], 'Added')
© www.soinside.com 2019 - 2024. All rights reserved.