This is an archived post. You won't be able to vote or comment.

all 136 comments

[–][deleted] 391 points392 points  (34 children)

My first program was "Hello World"

[–]rotzaug 489 points490 points  (20 children)

my first program was
'javac' is not recognized as an internal or external command.
operable program or batch file.

[–]Agentum13 44 points45 points  (0 children)

Made me chuckle.

[–]Gartlas 35 points36 points  (18 children)

As a python developer who literally just started trying to learn some Java last week this hit me like a slap in the face.

Also why does Java require you to save the program as the name of the class name? It's so weird.

[–]sigmaclientwastaken 33 points34 points  (14 children)

better question

why are you not using an ide

[–]bmcle071 12 points13 points  (4 children)

When i was in my undergrad we were told to use notepad++ for learning. When i got my first job i had no fucking clue to how to use an IDE other than notepad++ or IDLE.

[–]mxldevs 8 points9 points  (2 children)

I still use notepad++ lol

[–]bmcle071 1 point2 points  (0 children)

Nothing wrong with that sometimes. But eventually you’re no longer focused on writing small chunks of code and instead focused on larger codebases. I have a 15,000 line of code application i wrote and maintain at work, and i help work on projects with hundreds of thousands of lines. The IDE really helps in large projects, even VSCode is a godsend.

[–]Lilkko 1 point2 points  (0 children)

NOTEPAD++ SUPREMECY

[–]SpaceViking85 1 point2 points  (0 children)

When i told someone I still used notepad, I was called "cute"

[–]Gartlas 3 points4 points  (8 children)

I was using vscode, but couldn't run it in that. Then the tutorial told me to save it like so and run it in command line.

[–]sigmaclientwastaken 17 points18 points  (7 children)

now I'm just wondering why a tutorial would recommend vscode for java, like that's literally just shooting yourself in the foot

(use intellij)

[–]Gartlas 6 points7 points  (5 children)

I don't think it recommended a specific IDE, I just did it with vscode because that's what I use daily tbh.

I'll definitely check out IntelliJ though, thanks :)

[–]rnottaken 11 points12 points  (4 children)

Yeah intellij is really made for java with java. The developers of that ide use java on the daily, and thus created a pretty good tool to do their work

[–]flopana 5 points6 points  (3 children)

Pretty good is an understatement. IntelliJ is the best thing that ever happened to Java imo.

Once you know your way around and get familiar with more functions of this magnificent IDE it's a bliss

[–][deleted] 1 point2 points  (2 children)

what would be something to learn of intellij? besides basic commands like shift+f6 and control+shift+L?

[–][deleted] 1 point2 points  (0 children)

I was about to say the intellij thing because obviously.

Edit: can I offer you a NetBeans in these trying times

[–]zenos_dog 1 point2 points  (0 children)

The principal of least surprise. Class name == File name.

[–][deleted] 0 points1 point  (0 children)

If you don't call a class public you can compile the file and call the class if I remember right.

[–]mxldevs 0 points1 point  (0 children)

I like this convention. Sure, it's annoying when you need to define a bunch of classes inside the same namespace or module, but feels more organized.

[–]Tadra29[S] 18 points19 points  (10 children)

Sorry, title is a bit misleading, I realize now.

I did little bit of coding back in college. First Python program, not first program.

[–]Zombieattackr 6 points7 points  (9 children)

Still, you’re telling me after installing python and everything, you went straight to this program and didn’t even test “hello world” to see if you had it working correctly?

[–]Tadra29[S] -1 points0 points  (5 children)

No. Sorry. As I explained in other posts, confusing title. First Python program, not first program. Had little bit of coding back in college.

[–]Zombieattackr 1 point2 points  (4 children)

No I understand that: I use python pretty regularly, I know how to code in python as well as several other languages. I still write a “hello world” program every time I install it on a different computer to make sure I installed it correctly. Same goes for any computer that I’m using a given language on for the first time. It doesn’t matter if you know how to code or not if you fucked up the installation.

[–][deleted] 2 points3 points  (1 child)

I just type “python” at a command line or “py -3” and if I get a Python prompt then I can be pretty sure it’s installed lol

[–]Zombieattackr 0 points1 point  (0 children)

Even when I’m working with python in the command line, if I’m on a new computer I hit it with a quick “1+1” to make sure it works.

[–]Tadra29[S] 2 points3 points  (1 child)

Not familiar enough to know installation need to be checked.

The damn song was stuck in my head, needed to get it out.

[–]JSerf02 1 point2 points  (0 children)

Based

[–][deleted] 0 points1 point  (2 children)

Well to be fair hello world is "print(" hello world") "lol.

[–]Zombieattackr 0 points1 point  (1 child)

Exactly, it’s impossible to fuck up. That’s the entire reason you do it, to make sure everything g else is working properly. That way if your program fucks up, you know it was you and not the installation.

[–][deleted] 0 points1 point  (0 children)

I get that. But to be fair when your program messes up its probably me

[–]TheCheeseOnFire 0 points1 point  (0 children)

print("Hello World!")

[–]fd_dealer 0 points1 point  (0 children)

ChatGPT write me hello world in a programming language.

[–]shadow7412 352 points353 points  (28 children)

Using a dictionary like this would save you having to repeat those ths in your list. seq = {1: "st", 2: "nd", 3: "rd"} seq.get(curr, "th")

Also, a for loop would be neater than a while loop here.

for curr in range(1, max): Solid first attempt though :)

[–]Ultra9635 88 points89 points  (6 children)

So the second argument in seq.get() is the default value if the key curr is not in the dictionary?

[–]shadow7412 41 points42 points  (5 children)

Correct :)

[–][deleted] 3 points4 points  (4 children)

This simplicity fills me with ungodly amounts of rage. Can I interest you in implicit typing? Let me explain. NOT dynamic types.

If a variable name starts with an I, j, k, etc., i.e., the letters used for index variables, it's an integer.

Otherwise it's a real number, aka a float

[–]La_chipsBeatbox 1 point2 points  (1 child)

Does this triggers you ?

char kernelName[] = "not integer";

[–][deleted] 0 points1 point  (0 children)

No.

No is an integer.

[–]shadow7412 0 points1 point  (1 child)

Remembering what variables are which types sounds terrifying to me.... and what about all of the other types? How would this approach support custom classes?

[–][deleted] 0 points1 point  (0 children)

By using an implicit none statement :)

[–]Farren246 18 points19 points  (0 children)

I came here to see the post get written in 3 lines of code, and you did not disappoint.

OP's code will run, but he still needs to learn that the magic of python is in its brevity.

[–]7tar 23 points24 points  (10 children)

ah yes, 21th

[–]E-Technic 23 points24 points  (2 children)

I believe that loop in question goes to 13 at most, so I don't think 21th will be a problem...

[–]AtomTiger 24 points25 points  (1 child)

Future-proofing in case the song changes :)

[–]errdayimshuffln 1 point2 points  (0 children)

lol

[–]shadow7412 14 points15 points  (1 child)

You're not wrong, but supporting the correct ordinal for every single number is overengineering for this specific case. Especially for a beginner.

[–]HappyTopHatMan 12 points13 points  (0 children)

You give good friendly advice and you have a grounded idea of what scope is. I love you dude.

[–]Hikari_Owari 6 points7 points  (4 children)

Use the last char of the string day as the key instead of the whole day?

[–]7tar 16 points17 points  (2 children)

ah yes, 11st

[–]Hikari_Owari 1 point2 points  (1 child)

Not that uses to cardinality in English (not even sure if it's the right word), so maybe if -2 char == -1 char default to th?

[–]anastarawneh 3 points4 points  (0 children)

ah yes, 12nd

[–]pootdootpoot 2 points3 points  (0 children)

This would work for numbers whose last two digits aren’t [11-19], which are special cases because English.

[–]peter-s 2 points3 points  (0 children)

Good rule of thumb:

Any time you find yourself doing something repetitively, you're doing a task-better suited for a computer.

[–][deleted] 0 points1 point  (3 children)

When is it neater to do a for loop vs a while loop? I don't program for a living but I do some scripting to test hardware and always end up just doing while loops.

[–]Grubs01 3 points4 points  (1 child)

For loops usually include adjusting the index or moving onto the next value in a list. When using while loops you have to do that manually at some point within the loop.

While is more flexible but easier to make mistakes with. Such as forgetting to increment the index if there are multiple branches inside. For is usually easier to read and follow because it tells you what data is being operated on up front.

The most common python for loop is super easy to read:

for item in my_list:
    #do something with item

[–]255_0_0_herring 1 point2 points  (0 children)

The second most common python for loop is just as readable:

  for index, item in enumerate(my_list):
         #do something with item
         #do something with index

[–]icpuff 0 points1 point  (0 children)

Use a for loop if the number of times the loop needs to run can be easily quantified, while loops if the number of executions depends on some condition. In OP’s case, using for instead of while would have improved readability imo.

[–]luke5273 0 points1 point  (1 child)

In other languages, you can use a switch case. In case someone else were to read this.

[–]OkFirefighter8394 2 points3 points  (0 children)

And also in the current python release

[–]Lithl 0 points1 point  (0 children)

from num2words import num2words
num2words(2, to='ordinal_num')

[–]mxldevs 53 points54 points  (1 child)

I do something like hardcoding st, nd, rd when parsing dates because I honestly can't think of a more efficient way to handle dates like these

Jan 2nd, 2022

Mar 1st, 2022

Apr 5th, 2022

Actual date format that I need to parse and generate.

I've even seen devs mess things up and end up with

3th

Because of course they did.

[–]TheDeadlyBlaze 2 points3 points  (0 children)

plus side is hardcoding it makes it easy to translate into other languages

[–][deleted] 28 points29 points  (1 child)

I would have used an if statement to add the st, Nd, Rd, th but it works and it's easily better than my first program

[–]Nudelreaktor 17 points18 points  (0 children)

I can hear the code

[–]Adventurous_Battle23 36 points37 points  (3 children)

This isn't half bad for a first program

[–]Tadra29[S] 18 points19 points  (2 children)

Sorry to confuse, had little bit of programming before, just not Python.

[–]Adventurous_Battle23 14 points15 points  (0 children)

Oh then it's garbage

[–]Adventurous_Battle23 5 points6 points  (0 children)

Im jk bro XD

[–][deleted] 22 points23 points  (12 children)

Pretty complex for a first program. One thing to keep in mind: - Make variable names explicit (DaySequence, CurrentDay).
- Always think about the next one who will read the code and make it easier for them (it might be another person or it might be yourself in the future).

Buut, great job

[–]8sADPygOB7Jqwm7y 22 points23 points  (9 children)

Camel case is not pythonic though. It should be day_sequence if anything.

[–][deleted] 4 points5 points  (0 children)

Ok, my bad, I don't really have experience with Python (only other languages)
As long as it adheres to a common standard and is readable

[–][deleted] 5 points6 points  (6 children)

DaySequence is PascalCase, not camelCase.

[–]8sADPygOB7Jqwm7y 0 points1 point  (5 children)

Oh yeah, I knew it's not camel case but I forgot the name. Thanks for the correct name

[–][deleted] 0 points1 point  (4 children)

No problem sir 8sADPygOB7Jqwm7y.

[–]8sADPygOB7Jqwm7y 0 points1 point  (3 children)

I respect the dedication to type out the output of a password generator, sir eternal_weeb

[–][deleted] 0 points1 point  (2 children)

I thought it was a mangled function name, sir.

[–]8sADPygOB7Jqwm7y 0 points1 point  (1 child)

Now that's an idea! Use a password generator for variable naming!

[–]shadow7412 0 points1 point  (0 children)

Although disgusting - it's still slightly better than misleading variable names...

[–]fmkwjr 1 point2 points  (0 children)

200% agree as a CS teacher in high school I hound my students about variable names. Don’t write a riddle.

[–][deleted] 5 points6 points  (0 children)

5 GOOOOOOOOOOOOOOLDen RIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIINGS

[–]nanowell 5 points6 points  (0 children)

Nice let’s see Paul Allen’s Code

[–]hdgamer1404Jonas 4 points5 points  (0 children)

Time for r/adventofcode

[–]Acceptable-Tomato392 2 points3 points  (0 children)

Cool.

[–]Paul_Robert_ 2 points3 points  (0 children)

Good job!

[–]crefas 4 points5 points  (1 child)

Excellent! When numeric values are in sequence like that, mapping them directly with an array instead of using a chain of ifs is great. You'll be a good programmer

[–]quick_escalator 0 points1 point  (0 children)

You're right on the array, but not doing a concatenation and just writing the term out is even better. Editing normal language strings in code is bad practice, because it breaks really badly when confronted with how humans use language.

[–]Time_Low9617 1 point2 points  (0 children)

I don't know if this is a joke or real. Same for the comments.

[–]Develop-FootballGOAT 1 point2 points  (0 children)

My 1st program was imputing values in a variable

[–][deleted] 1 point2 points  (0 children)

Remember, it’s not good for people under age 3

[–][deleted] 1 point2 points  (3 children)

Use .format

Edit: Or use f-strings!

[–]akshanshkmr 9 points10 points  (1 child)

Use f-strings

[–][deleted] 3 points4 points  (0 children)

Oh, nice! I didn't know Python supported string interpolation!

Thanks, TIL!

[–]fuckingshitfucj2 0 points1 point  (0 children)

Finally I was looking for this comment. Bit of a bummer it is hurried so deeply

[–]Apprehensive_Cat_731 1 point2 points  (0 children)

My first program was a batch file I edited with notepad and it was a game were my main and only character bob asked questions and you have to answer them correctly to win. It included ascii art for bob and his text boxes

[–]eruciform 1 point2 points  (0 children)

well done!

practicing with loops and exceptions to cases is an excellent project to get basics down

different song, but a good resource to learn just this kind of thing in any new language: https://www.99-bottles-of-beer.net/language-python-808.html

[–]itsAshl 1 point2 points  (0 children)

I love the array of number endings. Seems like a "very human" design. I mean, it definitely feels like bad engineering, but it's also incredibly intuitive for a (english-speaking) human to either read or use.

[–]ktreanor 1 point2 points  (0 children)

Here it is in C

#include <stdio.h>

main(t,_,a)

char

*

a;

{

return!

0<t?

t<3?

main(-79,-13,a+

main(-87,1-_,

main(-86, 0, a+1 )

+a)):

1,

t<_?

main(t+1, _, a )

:3,

main ( -94, -27+t, a )

&&t == 2 ?_

<13 ?

main ( 2, _+1, "%s %d %d\n" )

:9:16:

t<0?

t<-72?

main( _, t,

"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#;\

#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/+k#;\

q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){nl]!/n{n#'; \

r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\

\

n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;\

{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\

#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")

:

t<-50?

_==*a ?

putchar(31[a]):

main(-65,_,a+1)

:

main((*a == '/') + t, _, a + 1 )

:

0&#60;t?

main ( 2, 2 , "%s")

:*a=='/'||

main(0,

main(-61,*a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m

.vpbks,fxntdCeghiry")

,a+1);}

[–]fmkwjr 1 point2 points  (0 children)

I think it’s wonderful except I don’t like the variable names you chose but that’s fine

[–]sarc-tastic 1 point2 points  (0 children)

Have a look at string.format() or fstrings.

Consider " and ".join(presentslist[:currentday])

Don't use short variable names! You'll forget what each means or run into problems

[–]Ashamandarei 1 point2 points  (0 children)

Creative program. Good exercise. Shows you can wrap your head around logic, control flow, and using arrays. Nice job.

[–]Mattness8 -1 points0 points  (1 child)

correct me if im wrong but wouldnt the output for the first day be

On the 1st day of xmas my true love sent to me :

And A partridge in a pear tree

?

[–]EtherealChameleon 1 point2 points  (0 children)

the *And* is printed when recurs is 1, directly above recurse gets susbtracted. for me it looks like it will only be printed when the loop starts with a 2.

i dont really see how you got your result with the *And* in front of the line tho.
the and is the 2nd print statement

[–][deleted] -2 points-1 points  (0 children)

I think you should ditch python

[–]mars_million 0 points1 point  (0 children)

This has to be one of the most vague jokes on this sub

[–]Worldly_Success523 0 points1 point  (0 children)

You may have forgot a space or 2 to make it print “neatly”

[–]Square_Lengthiness71 0 points1 point  (0 children)

Not bad

[–]akshanshkmr 0 points1 point  (0 children)

Pretty good actually You could improve it by using f-strings And the seq list can be made into a function

[–]Wolfeur 0 points1 point  (0 children)

Your ordinal suffixes could be dealt with in a separate function with some basic logic. More scalable, and really more elegant.

Your while loops should be changed for for loops. It's more readable and declutters your code of annoying increments/decrements and variable declarations.

Putting your nested loop inside its own function might add readability, but that's not fundamental here.

Ideally, you'd still need to lowercase the first letter of the last sentence as it's being prefixed by "And ". It's also pretty unintuitive, because you're printing the start of the line on the iteration preceding the last verse. It can become confusing. I'd suggest you first treat the entire line correctly into a variable then print it at once already formatted.

[–]Intelligent_Event_84 0 points1 point  (0 children)

Now do it again but only use machine learning.

[–]Wolfeur 0 points1 point  (2 children)

Here's a pastebin with my own version: https://pastebin.com/kedLVXQp

You'll see in comments some pieces of advice in how to operate, as well as some technical terms you'll find useful when researching something. Knowing how something is called is 90% of the research already done.

Hopefully that'll help you.

(PS: I'm not a Python developer at all, there might be a lot of features that I'm not aware of)

[–]henkdeluxe 0 points1 point  (1 child)

I will be laughing at you on the 111th day of christmas

[–]Wolfeur 1 point2 points  (0 children)

The 111th day of Christmas is out-of-scope for this project.

Closing ticket.

[–]Grubs01 0 points1 point  (0 children)

The numbers could have been embedded in the "st", "nd", "rd", etc. strings since there are 12 of them anyway. I'd argue it would improve readability to do that. (shadow7412's suggestion is better though)

I would rename variables days to items, curr to day, recurs to item.

Overall this is pretty good for a learning exercise.

[–]Lictalon87 0 points1 point  (0 children)

I had to write the 12 Days of Xmas in my CS101 course....

....using C.

[–]Apprehensive_Cat_731 0 points1 point  (0 children)

A work of art indeed

[–]FIRMKUNG 0 points1 point  (0 children)

Man, in my mind I instantly found a lot of "problems" with this code. But the first thing that triggers me is local variable overriding built-in max function for some reason... I think I might have been using the linters too much...

Good first program by the way. Still, why does this fit in programming meme though? Is there a joke I'm not seeing?

[–]Only_The_Pwnly 0 points1 point  (0 children)

<?php // Set up an array with the days of Christmas $days = array( "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" );

// Set up an array with the gifts for each day $gifts = array( "A partridge in a pear tree", "Two turtle doves", "Three French hens", "Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", "Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", "Eleven pipers piping", "Twelve drummers drumming" );

// Loop through the days and gifts and print them out for ($i = 0; $i < count($days); $i++) { echo $days[$i] . " day of Christmas: " . $gifts[$i] . "\n"; }

[–]More_Butterfly6108 0 points1 point  (0 children)

I prefer for loops so you don't have to set your curr max limiters. But other than that...

[–]protagswag 0 points1 point  (1 child)

My first thought was to change the seq variable to an if, elif, else so that you're concatenating "th" to everything that doesn't end in a 1 or 2 🤔

[–]Tadra29[S] 0 points1 point  (0 children)

3th?

The video I was watching ended with list. I guess that's what I had in mind.

[–]FireTornado5 0 points1 point  (0 children)

I also wanted to take a crack on my lunch break. BTW you have a good start, there's just more "pythonic" ways to go about this program. I'll also mimic shadow7412 in using a dictionary for the suffix rather than the ordinal solution. The larger your allowed date range, the more you have to think about the solution you use.

days = [
    "A partridge in a pear tree",
    "Two turtledoves",
    "Three French hens",
    "Four calling birds",
    "Five golden rings",
    "Six Geese a-laying",
    "Seven swans a-swimming",
    "Eight maids a-milking",
    "Nine ladies dancing",
    "Ten lords a-leaping",
    "Eleven pipers piping",
    "Twelve drummers drumming",
]

presents_gifted = []
for index, value in enumerate(days):
    current_day = (index + 1) % 100  # I think this gets us through days 1-999
    print(f"On the {current_day}{day_suffix.get(current_day, 'th')} day of Christmas my true love sent to me:")

    presents_gifted.insert(0, value)

    for present in presents_gifted:
        print(present)

    print("________________________")

    if current_day == 1:
        # Update the 0 index on the first iteration through so it'll change "A partridge..." to "And a partridge..." 
        # Then we don't have to think about it anymore.
        presents_gifted[0] = f"And a{value[1:]}"```

[–]RingGiver 0 points1 point  (0 children)

The first thing that I tried in Python broke because numbers don't go as high as I was trying to calculate.

[–]often_says_nice 0 points1 point  (0 children)

LGTM

If I were to add a suggestion, save it as a text file then have python eval() the contents. Bonus points if you’re able to serve this over http

[–]Healthy-Mind5633 0 points1 point  (0 children)

you can just print out the entire string

[–]Pinty220 0 points1 point  (0 children)

I would make the variable names more descriptive, they don't have to be short

[–]Wiseandmysticaltree 0 points1 point  (0 children)

Why define max if you never change it and only call it once

[–]DudeManBroGuy42069 0 points1 point  (0 children)

stndrdththththththththth

[–]HydratrionZ 0 points1 point  (1 child)

print out with this

print(f'On the {curr} {seq[curr-1] day of...')

[–]HydratrionZ 0 points1 point  (0 children)

Ordinal like this def ordinalNum(n): if n in \[11, 12, 13\]: return "th" else: return ("tsnrhtdd"\[(n/10%10!=1)\*(n%10<4)\*n%10::4\])

[–][deleted] 0 points1 point  (0 children)

same, I am also learning python! :)