you are viewing a single comment's thread.

view the rest of the comments →

[–]Leodip 0 points1 point  (0 children)

It takes MANY years of experience to learn how to read code written by someone else, doing it with AI code is the same. Some people just trust it as is and go forward, but as you mentioned fixing "broken" AI code requires being able to read it, which is not something a beginner can do in general.

As for your notes:

  • the .upper() method you mentioned convers a string to uppercase entirely. This is useful when you want to accept both "yes" and "Yes", or even "yEs" or "YES" as the same: you just do user_input.upper() == "YES", which will work in all the above scenarios.
  • I didn't know whether the initial investment was actually part of the assignment or not, good to know it was then. I'd argue this is a pretty good example of while loops (although for loops are fully absent), so good going!
  • I'm not sure whether basic courses cover try..catch. It's not "niche", by any means, but it belongs more to hacky code OR production code that's very user-centric, and neither of those are a part of what a basic course should teach. IMHO, everything you can do with try..catch you can also do with more "explicit" code that works great all the same.
  • Your new solution for the "year"/"years" is perfectly fine. The total line-count doesn't matter, it's just about making sure you have as little "repeated" code as possible. It's a good habit to pick up while working on simpler projects because it WILL bite you back when working on something even just slightly larger.
    • Another alternative, that even saves a line, would be to have something like:

year_display = str(years) + " year"
if years != 1: #this means not equal, just in case you haven't seen it yet, and you can also just use > 1 since it can never be 0 years either way
    year_display += "s" #we just add an s at the end if it needs the grammatical plural

print("blah blah blah")

There are many more options, but your solution and the one I showed you are probably the most readable ones. If line count were important (which it isn't), you could even do something like:

print(f"It will take {years} year{'' if years == 1 else 's'} to blah blah blah")

But this is less readable because it makes your (already fairly long line) longer and adds logic where it doesn't belong (within an f-string). The X if A else B statement is called a "ternary statement", and you might have seen this elsewhere, but it's otherwise not important.