#πŸ”’ i need help

26 messages Β· Page 1 of 1 (latest)

light horizon
#

hey im very new to python and i got confused with the for loop, i want to print values that i input and got added to list which look like this
foods=["pizza","burger","fries"]
prices=[15,20,11]
then i want to print this
print (f"You buy {food} with the prices of: {price}")
now i use for loop to print them but it didnt work like i imagine
for food in foods: print(foods,end=" ") for price in prices: print (f"You buy {food} with the prices of: {price}")

broken carbonBOT
#

@light horizon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

light horizon
#

idk if i can explain in an easy way to understand

whole atlas
#

if you have to use lists and for loops here, you need to use the index of the lists and iterate through them

opaque goblet
#

you can also use zip

#

!d zip

broken carbonBOT
#
zip

zip(*iterables, strict=False)```
Iterate over several iterables in parallel, producing tuples with an item from each one.

Example...
opaque goblet
#

!e

foods=["pizza","burger","fries"]
prices=[15,20,11]

for food, price in zip(foods, prices):
   print(food, price)
broken carbonBOT
light horizon
#

thanks youπŸ™πŸ™

opaque goblet
#

nw

modern whale
#

!e - if you're doing it without zip() (which is more cumbersome) you would do something like

foods = ["pizza", "burger", "fries"]
prices = [15, 20, 11]

for i in range(len(foods)):
    print(foods[i], prices[i])
broken carbonBOT
hollow tiger
#

!e
Or you can use range based for loops to do the same


foods=["pizza","burger","fries"]
prices=[15,20,11]

for i in range(len(foods)):
  print(foods[i], prices[i])
hollow tiger
modern whale
hollow tiger
modern whale
hollow tiger
broken carbonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.