It seems you have two unrelated questions here.
The first is whether your calculateTutitionIncrease
function does the right thing to create an list of cost values over time.
It is not bad, exactly, but it could be better. To start with, you could use a for
loop over a range
, rather than a while
loop testing an integer that you manually increment. An even more “Pythonic” style of code is to use a list comprehension when you want to create a list of items from some other sequence (like the range of year numbers). This requires a slightly different mathematical calculation in this case, but the results should be the same:
costs_per_year = [cost * (1+increase)**year for year in range(1, years+1)]
Your second question seems to be about whether you should keep your code for asking if the user wants to quit in a separate function. This is fairly subjective, so there really isn’t a single right answer. I think there are two good reasons that you might want to have it in its own function.
First, it can help make your other code simpler, especially if most of the other work is also separated out into functions:
def main():
while True:
input_data = get_input()
results = calculate_results(input_data)
display_results(results)
if ask_to_exit():
break
You don’t see much benefit to this currently, because your main
function is already quite long and complicated.
The second reason is if you might want to call it from multiple different places in your other code. For instance, if you had two different loops, it might make sense to use the same function to ask the user if they were done in each of them. This doesn’t currently apply to your code though, since you only call your exitProgram
function in exactly one place.
So, in the end, neither of the reasons speak strongly to your situation. That doesn’t necessarily mean that you should stick the contents of exitProgram
back into main
though. If you think you might change the other code (for instance, by factoring out the other steps you go through in main
into functions of their own), it might make sense to keep exitProgram
as a separate function too.
A few further suggestions:
Pick a different name for your exitProgram
function, if you choose to keep it. Functions are usually given names that are verbs describing what they do. Your exitProgram
function is misleading though, since it doesn’t actually cause the program to exit (it just returns what the users desire is, regarding exiting). I’d suggest that you make it return a bool
value too, rather than a y
or n
string that the caller will need to test against.
Also, Python code convention is for functions and variables to be named in lower_case_with_underscores
format, rather than camelCase
like you are currently using. See PEP 8 for more Python style suggestions.
2
solved I’m very new to programming. Is this acceptable? [closed]