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}")
#π i need help
26 messages Β· Page 1 of 1 (latest)
@light horizon
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.
Closes after a period of inactivity, or when you send !close.
idk if i can explain in an easy way to understand
if you have to use lists and for loops here, you need to use the index of the lists and iterate through them
zip(*iterables, strict=False)```
Iterate over several iterables in parallel, producing tuples with an item from each one.
Example...
!e
foods=["pizza","burger","fries"]
prices=[15,20,11]
for food, price in zip(foods, prices):
print(food, price)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | pizza 15
002 | burger 20
003 | fries 11
this also works ^
is this what you wanted?
nw
!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])
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | pizza 15
002 | burger 20
003 | fries 11
!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])
look just above π
Lol, we both gave the same solutions
yeah, character for character unless you count spaces for formatting π
Yeah, I was actually stunned, I thought how my message got sent without pressing Enter
also, the message must start with the !e to work, that's why i wrote my comment after on that same line
by magic π
Oh I got it, so thats why my codeblock wasn't executed, thanks
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.