pythonGo
pythonGo

Reputation: 173

dictionary list cocatenation from multiple user input while true

I am totally new to python. I am trying to do a dictionary concatenation from user input and print the result. I have searched the internet for all solutions, but no solutions. Please, help.

shoeCat = {1: 'Adidas', 2: 'Alexander McQueen', 3: 'Converse', 4: 'Fila', 5: 'Kids/Teens', 6: 'Men’s Kicks', 7: 'New Balance', 8: 'Nike', 9: 'OFF-White', 10: 'Puma', 11: 'Select Brands', 12: 'Slides', 13: 'Under Armour', 14: 'Women’s Kicks'}
mycat1 = input("Enter 1st category: ")
cat1 = [shoeCat[int(x)] for x in mycat1.split()]
print(cat1)
mycat2 = input("Enter 2nd Category: ")
cat2 = [shoeCat[int(x)] for x in mycat2.split()]
print(cat2)
final= cat1 + cat2
print(*final, sep = " | ")

How do i make the code to ask for infinite multiple user inputs whith the whiile true if statement.

while True:
print ('Enter Category: (Press enter to generate.)')
mycat1 = input("")
if mycat1 == '':
    break

I want to achieve these:

Enter 1st category: 1
['Adidas']
Enter 2nd Category: 2
['Alexander McQueen']
Enter 3rd Category: 6
['Men’s Kicks']
Enter 4th Category: 11
['Select Brands']
Enter 2nd Category: 13
['Under Armour']
Adidas | Alexander McQueen | Men’s Kicks | Select Brands | Under Armour

Please how do i make python3 get these result for me.

Upvotes: 0

Views: 63

Answers (2)

Anoop R Desai
Anoop R Desai

Reputation: 720

Keep a list to store the values that you have printed:

shoeCat = {1: 'Adidas', 2: 'Alexander McQueen', 3: 'Converse', 4: 'Fila', 5: 'Kids/Teens', 6: 'Men’s Kicks', 7: 'New Balance', 8: 'Nike', 9: 'OFF-White', 10: 'Puma', 11: 'Select Brands', 12: 'Slides', 13: 'Under Armour', 14: 'Women’s Kicks'}
lst = []
while True:
    mycat1 = input("Enter Category: (Press enter to generate.)")
    if mycat1 == '':
        break
    lst.append(shoeCat[int(mycat1)])
    print(shoeCat[int(mycat1)])
print(*lst, sep=" | ")

lst stores the values that you have printed out. You can then just plainly print out the list with the required separator.

Note that this assumes the user always enters a valid integer input that is in shoeCat. Additional checks need to be in place to make sure it handles edge cases.

Upvotes: 1

alec
alec

Reputation: 6112

You can append the user input to the same list every time. The while loop would accept input, if it's an empty string then break and print the list. Otherwise add the input to the list and print the value. The trickiest thing in your question is actually getting the ordinal number representation (1st, 2nd, 3rd, etc), which there is an answer to here.

shoeCat = {1: 'Adidas', 2: 'Alexander McQueen', 3: 'Converse', 4: 'Fila', 5: 'Kids/Teens', 6: 'Men’s Kicks', 7: 'New Balance', 8: 'Nike', 9: 'OFF-White', 10: 'Puma', 11: 'Select Brands', 12: 'Slides', 13: 'Under Armour', 14: 'Women’s Kicks'}
ordinal = lambda n: "%d%s" % (n, "tsnrhtdd"[(n // 10 % 10 != 1) * (n % 10 < 4) * n % 10::4])
i = 1
final = []
while True:
    mycat1 = input(f"Enter {ordinal(i)} category: ")
    if mycat1:
        final.append(shoeCat[int(mycat1)])
        print(final[-1:])
        i += 1
    else:
        print(' | '.join(final))
        break

Upvotes: 2

Related Questions