Home > OS >  How do I choose an item in my list with an input command?
How do I choose an item in my list with an input command?

Time:06-17

So Im trying to make an easy candybag. With one option to choose candy in the bag and one option to randomize the bag. The random bag works, but how do I choose specific candy from the list, using an input from the user?

This is how i wrote that part.

os.system("cls")
customer_bag = []
candy_types = [["Sour strings", 2], ["Red Racecars", 2], ["Raspberry Boats", 1], ["Banana Skids", 3], ["Jungle Roar", 1], ["The Oh no Candy", 0,5]]
candy_types.sort()
for types in candy_types:
    print(types)

print("\nDo you want to choose candy or buy the shuffled candybag?")
candy_bag = int(input("1. Choose Candy\n2. Shuffle!!!"))

if candy_bag == 1:
    os.system("cls")

    candy_choice = candy_bag[]
    input()
    

elif candy_bag == 2:
    os.system("cls")

    print(random.choice(candy_types))
    print(random.choice(candy_types))
    print(random.choice(candy_types))
    print(" ")
    input("Press enter to return to Menu!")'''

CodePudding user response:

Add number of item while printing. And then just do another input() with number of item.

import os

os.system("cls")
customer_bag = []
candy_types = [["Sour strings", 2], ["Red Racecars", 2], ["Raspberry Boats", 1], ["Banana Skids", 3], ["Jungle Roar", 1], ["The Oh no Candy", 0,5]]
candy_types.sort()
for i, types in enumerate(candy_types, start=1):
    print(i, types)

print("\nDo you want to choose candy or buy the shuffled candybag?")
candy_bag = int(input("1. Choose Candy\n2. Shuffle!!!").strip())

if candy_bag == 1:
    os.system("cls")

    candy_choice = int(input("1. Choose Candy:"))
    candy_bag = candy_types[candy_choice - 1][0]
    print(candy_bag)


elif candy_bag == 2:
    os.system("cls")

    print(random.choice(candy_types))
    print(random.choice(candy_types))
    print(random.choice(candy_types))
    print(" ")
    input("Press enter to return to Menu!")

CodePudding user response:

So Im trying to loop this candy picking... 3 different candies. So far ive come up with 3 of the same. Anyone with a solution to it? Ive put the loop

candy_choice = int(input("1. Choose Candy:"))
candy_bag = candy_types[candy_choice - 1][0]
for sweets in range(3):
    print(candy_bag)
input()

Though this only repeats the first choice of candy.

  • Related