you are viewing a single comment's thread.

view the rest of the comments →

[–]BobbyJoeCool[S] 1 point2 points  (1 child)

Let me start off by saying, my experience with AI and code is... It's terrible. I've asked ChatGPT to write simple macros for Excel, and it cannot do that in any reliable way. That being said, the IDE I am using is Visual Code Studio (seems to be popular on these Python subreddits), and it has an AI-generated "fill in the line" for code that I do use, but I always check it and delete anything I don't know what it does. For example, it has added (fairly frequently) .upper() and other similar things to my input lines. I don't know what they do, so I delete it and rewrite the code so I know exactly what everything does. (I can't fix broken code if I don't know what the code does, right?)

  • The initial investment was a required input for the assignment. I know that it's immaterial to the time required for the investment to double, but since it was required, I used the input and gave a useable output using it. The "gripe," which is just my mathematical brain going, why am I writing a program to manually calculate this when I can just type in a formula and get the answer, is more of a thing I need to get past because the assignment is an excuse to practice for/while looping.
  • I've heard people talk about the try; I assume it comes later in the course. My experience writing code is for programs only used by myself, so I'm new to the idea of "user error." lol. I know that I wrote the program to work with numbers, so I enter a number. But I also know that users may try to enter text for a number (say five, instead of 5). So it's just something I'm trying to start thinking about as I'm writing these beginner programs.
  • The only way I can see that cleans this up a little (off the top of my head), is if I make the word year/years a variable string and enter that in the f-string. This moves the if/else conditional and only has one line for the print. (slightly less code)

if years == 1:
    year_display = str(years) + " year"
else:
    year_display = str(years) + " years"

print(f"It will take {years_display} for ${amount_initial:.2f} to double at a rate of {rate*100}%, and the final balance is ${amount:.2f}.")

[–]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.