Найти - Пользователи
Полная версия: Подскажите пожалуйста, как присвоить значение(число) к ячейкам списка?
Начало » Python для новичков » Подскажите пожалуйста, как присвоить значение(число) к ячейкам списка?
1
eDn1
Вот код программы
 def fruits():
	for item2 in products_fruits:
		print(item2, end="\n")
def vegetables():
	for item3 in products_vegetables:
		print(item3, end="\n")
def chancelleries():
	for item4 in products_chancelleries:
		print(item4, end="\n")
print ("Добро пожаловать в Магазин продуктов!")
print("Выберите себе продукты: ", end='\n')
category = ["Фрукты", "Овощи", "Канцелярия"]
products_fruits = ["Бананы", "Яблоки", "Груши", "Апельсины", "Мандарины"]
products_vegetables = ["Капуста", "Картошка", "Морковь", "Помидоры", "Огурцы"]
products_chancelleries = ["Ручки", "Тетради", "Карандаши", "Фломастреы", "Пластилин"]
shoplist = []
shop = True	
while shop:
	for item in category:
		print(item, end="\n")
	print("Если желаете закончить покупки, нажмите 'q'")
	takecategory = input("Выберите категорию: ")
	if takecategory == "1":
		fruits()
		fruit = True
		while fruit:
			print("Если желаете вернуться в Категории, нажмите 'q'")
			takefruits = input("Выберите фрукты: ")
			if takefruits == "1":
				shoplist.append(products_fruits[0])
			elif takefruits == "2":
				shoplist.append(products_fruits[1])
			elif takefruits == "3":
				shoplist.append(products_fruits[2])
			elif takefruits == "4":
				shoplist.append(products_fruits[3])
			elif takefruits == "5":
				shoplist.append(products_fruits[4])
			elif takefruits == "q":
				fruit = False
			else:
				print("Такого фрукта в списке нет!")
			print("В вашей корзине" + str(shoplist), end="\n\n")
	elif takecategory == "2":
		vegetables()
		vegetable = True
		while vegetable:
			print("Если желаете вернуться в Категории, нажмите 'q'")
			takevegetables = input("Выберите овощи: ")
			if takevegetables == "1":
				shoplist.append(products_vegetables[0])
			elif takevegetables == "2":
				shoplist.append(products_vegetables[1])
			elif takevegetables == "3":
				shoplist.append(products_vegetables[2])
			elif takevegetables == "4":
				shoplist.append(products_vegetables[3])
			elif takevegetables == "5":
				shoplist.append(products_vegetables[4])
			elif takevegetables == "q":
				vegetable = False
			else:
				print("Таких овощей в списке нет!")
			print("В вашей корзине" + str(shoplist), end="\n\n")
	elif takecategory == "3":
		chancelleries()
		chancellery = True
		while chancellery:
			print("Если желаете вернуться в Категории, нажмите 'q'")
			takechancelleries = input("Выберите канцелярский товар: ")
			if takechancelleries == "1":
				shoplist.append(products_chancelleries[0])
			elif takechancelleries == "2":
				shoplist.append(products_chancelleries[1])
			elif takechancelleries == "3":
				shoplist.append(products_chancelleries[2])
			elif takechancelleries == "4":
				shoplist.append(products_chancelleries[3])
			elif takechancelleries == "5":
				shoplist.append(products_chancelleries[4])
			elif takechancelleries == "q":
				chancellery = False
			else:
				print("Такого канцелярского товара в списке нет!")
			print("В вашей корзине" + str(shoplist), end="\n\n")
	elif takecategory == "q":
		shop = False
	else:
		print("Такой Категории в списке нет!")
	print("Вы купили: ", shoplist, end="\n\n")
print("Спасибо за покупки!")
xam1816
eDn1
Подскажите пожалуйста, как присвоить значение(число) к ячейкам списка?
Вопрос не понятен, давайте подробнее на пальцах с примерами,или другими словами, что у вас на входе, и что хотите получить на выходе
py.user.next
  
def fruits():
    for number, item in enumerate(products_fruits, 1):
        print(number, item, end="\n")
 
def vegetables():
    for number, item in enumerate(products_vegetables, 1):
        print(number, item, end="\n")
 
def chancelleries():
    for number, item in enumerate(products_chancelleries, 1):
        print(number, item, end="\n")
 
print ("Добро пожаловать в Магазин продуктов!")
 
print("Выберите себе продукты: ", end='\n')
category = ["Фрукты", "Овощи", "Канцелярия"]
products_fruits = ["Бананы", "Яблоки", "Груши", "Апельсины", "Мандарины"]
products_vegetables = ["Капуста", "Картошка", "Морковь", "Помидоры", "Огурцы"]
products_chancelleries = ["Ручки", "Тетради", "Карандаши", "Фломастреы", "Пластилин"]
shoplist = []
shop = True    
while shop:
    for number, item in enumerate(category, 1):
        print(number, item, end="\n")
    print("Если желаете закончить покупки, нажмите 'q'")
    takecategory = input("Выберите категорию: ")
    if takecategory == "1":
        fruits()
        fruit = True
        while fruit:
            print("Если желаете вернуться в Категории, нажмите 'q'")
            takefruits = input("Выберите фрукты: ")
            if takefruits == "1":
                shoplist.append(products_fruits[0])
            elif takefruits == "2":
                shoplist.append(products_fruits[1])
            elif takefruits == "3":
                shoplist.append(products_fruits[2])
            elif takefruits == "4":
                shoplist.append(products_fruits[3])
            elif takefruits == "5":
                shoplist.append(products_fruits[4])
            elif takefruits == "q":
                fruit = False
            else:
                print("Такого фрукта в списке нет!")
            print("В вашей корзине" + str(shoplist), end="\n\n")
    elif takecategory == "2":
        vegetables()
        vegetable = True
        while vegetable:
            print("Если желаете вернуться в Категории, нажмите 'q'")
            takevegetables = input("Выберите овощи: ")
            if takevegetables == "1":
                shoplist.append(products_vegetables[0])
            elif takevegetables == "2":
                shoplist.append(products_vegetables[1])
            elif takevegetables == "3":
                shoplist.append(products_vegetables[2])
            elif takevegetables == "4":
                shoplist.append(products_vegetables[3])
            elif takevegetables == "5":
                shoplist.append(products_vegetables[4])
            elif takevegetables == "q":
                vegetable = False
            else:
                print("Таких овощей в списке нет!")
            print("В вашей корзине" + str(shoplist), end="\n\n")
    elif takecategory == "3":
        chancelleries()
        chancellery = True
        while chancellery:
            print("Если желаете вернуться в Категории, нажмите 'q'")
            takechancelleries = input("Выберите канцелярский товар: ")
            if takechancelleries == "1":
                shoplist.append(products_chancelleries[0])
            elif takechancelleries == "2":
                shoplist.append(products_chancelleries[1])
            elif takechancelleries == "3":
                shoplist.append(products_chancelleries[2])
            elif takechancelleries == "4":
                shoplist.append(products_chancelleries[3])
            elif takechancelleries == "5":
                shoplist.append(products_chancelleries[4])
            elif takechancelleries == "q":
                chancellery = False
            else:
                print("Такого канцелярского товара в списке нет!")
            print("В вашей корзине" + str(shoplist), end="\n\n")
    elif takecategory == "q":
        shop = False
    else:
        print("Такой Категории в списке нет!")
    print("Вы купили: ", shoplist, end="\n\n")
 
print("Спасибо за покупки!")

Добро пожаловать в Магазин продуктов!
Выберите себе продукты:
1 Фрукты
2 Овощи
3 Канцелярия
Если желаете закончить покупки, нажмите 'q'
Выберите категорию: 1
1 Бананы
2 Яблоки
3 Груши
4 Апельсины
5 Мандарины
Если желаете вернуться в Категории, нажмите 'q'
Выберите фрукты: 1
В вашей корзине['Бананы']

Если желаете вернуться в Категории, нажмите 'q'
Выберите фрукты: 2
В вашей корзине['Бананы', 'Яблоки']

Если желаете вернуться в Категории, нажмите 'q'
Выберите фрукты: q
В вашей корзине['Бананы', 'Яблоки']

Вы купили: ['Бананы', 'Яблоки']

1 Фрукты
2 Овощи
3 Канцелярия
Если желаете закончить покупки, нажмите 'q'
Выберите категорию: q
Вы купили: ['Бананы', 'Яблоки']

Спасибо за покупки!
eDn1
xam1816
Я хочу узнать, как допустим присвоить цену на продукты которые хранятся в этих ячейках
 products_fruits = ["Бананы", "Яблоки", "Груши", "Апельсины", "Мандарины"]
products_vegetables = ["Капуста", "Картошка", "Морковь", "Помидоры", "Огурцы"]
products_chancelleries = ["Ручки", "Тетради", "Карандаши", "Фломастреы", "Пластилин"]
AD0DE412
eDn1
Я хочу узнать, как допустим присвоить цену на продукты которые хранятся в этих ячейках
тогда не понятно зачем вы выбрали list a не dict
 products_fruits = {"Бананы": 80, "Яблоки": 120, "Груши": 240, "Апельсины": None, "Мандарины": 1200}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB