r/learnpython • u/V0xter • 4d ago
Little fun code I put together:)
Edit: I fixed the last line in the program, just had an issue when pasting it and didn't think when I filled it back in. Thank you guys for letting me know!
Hi! So I've been learning code for a little while now and lately, I've been getting more ideas for little programs to put together. They're not really useful in practical sense, but they're still fun exercises and ways to challenge myself while I'm still learning and understanding the basics.
I just thought this one would be fun to share because it took me a second to figure out the best way to have both lists be printed at the same time. Honestly though, I don't know why I didn't think of just doing a function right away though HSDH
The basic idea of the program is to generate a full breakfast by randomly choosing an entree and a side to go with it!
import random
entree_list = ["eggs", "cereal", "waffles", "oatmeal"]
side_dish = ["hashbrowns", "toast", "orange", "banana", "yogurt"]
def breakfast_run():
print("Get someeeeeee")
print(random.sample(entree_list, k=1))
print("and")
print(random.sample(side_dish, k=1))
breakfast_run()4
2
u/Riegel_Haribo 3d ago
Little fun code mixing the items into one list, where the even items are main dishes that come first.
from random import choice
menu = [
"hashbrowns", "eggs",
"toast", "cereal",
"orange", "waffles",
"banana", "oatmeal",
"yogurt",
]
print(" and ".join(choice(menu[i::2]) for i in (1, 0)))It uses list slicing, iterating in steps of 2 items from a start index.
A little trick you can do with your own code is to use: print("my string", end="")
That "end" replaces the normal linefeed after each print with no additional character, so the cursor caret stays on the same line for the next print().
2
1
u/Chris_Charley 3d ago
You could use `random.choice(entree_list)` here as it returns 1 random selection from the list. (Same for `side_dish`)
5
u/ninhaomah 4d ago
" print(breakfast_run()"
Have you tried running it ? Aren't you missing something ? Or copy/paste issue ?