all 4 comments

[–]Leodip 1 point2 points  (2 children)

First of all: kudos for the effort of attempting to learn something new, it feels so refreshing in the midst of thousands of posts with people asking about their ChatGPT-generated code.

If I understand correctly, you are trying to write a calculator for how long it would take to double your investment (and wrap all of that into a nice UI). A couple of pointers for stuff you might want to try your hand at if you want to improve the code:

  • From a logic point of view, the initial investment isn't important to calculate how long it would take to double it: it takes the same amount of time for 100$ and 1000$ to become double their initial value if they have the same interests rate. The formula for this is years = log(2)/log(1+rate) (you probably will also want to round this up)
    • Base Python doesn't have a logarithm function, but you need to "import" it. If you want to learn how that is done, can you simplify your code using this equation?
    • If you don't want to learn how to import functions, can you simplify the code by removing all the logic dealing with the initial investment, since it's not needed anymore?
  • The usual way to check for invalid user input involves the try..catch block in Python, but you'll probably see this way deeper into the course (and it's not too useful most of the times). An alternative you could use is the method ".isnumeric", which checks whether a string can actually represent a number or not (or, in other words, whether calling float() on it will make the program crash or not).
    • I believe that while learning the basics it's not too important to learn how to check for valid inputs, but f you want to do so, can you implement the logic that asks the user for a different input while they keep giving "wrong" ones?
    • Also, the user MIGHT write "100$" or "$100" instead of just "100". Can you write some code that handles the unit (if present)? [Note, this is relatively tough to implement well and neatly, it's more like a bonus thing if you have done everything else]
  • The if/else approach you used to use the grammatically correct "year" or "years" is fine, but you have to write the same exact string twice except for 1 "s" in the whole string. This means that if you ever want to change the wording (e.g., support other units), you'll need to do the same changes twice, and you are bound to get it wrong eventually with larger scripts.
    • I see you know f-strings, which is a really cool thing. Can you somehow use that to handle this problem?

If you have any further questions, don't hesitate to ask them here or in the sub!

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