r/learnpython • • 11d 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()
0 Upvotes

11 comments sorted by

View all comments

3

u/Riegel_Haribo 10d 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().

1

u/V0xter 10d ago

OOHH Okayyy! I'll have to play around with this soon..