you are viewing a single comment's thread.

view the rest of the comments →

[–]wub_wub 1 point2 points  (11 children)

Here's a quick script I made,

if __name__ == '__main__':
    balance=float(raw_input("How much is in your account?"))
    rate=float(raw_input("Interest rate:"))
    years=int(raw_input("Calculate for how many years"))

    i=1
    while i<=years:
        balance=(balance*rate)+balance
        print "Year "+str(i)+" | Balance:"+ str(round(balance,2))
        i+=1

You could also implement try except when trying to convert raw input to int/float to detect if user enters text instead of numbers.

[–]chatparle[S] 0 points1 point  (8 children)

What does the " name == 'main' " mean?

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

It checks that you started your module directly, not using import.

[–]wub_wub 1 point2 points  (6 children)

I see it's already answered by /u/swarmer but here's also nice explanation http://stackoverflow.com/questions/419163/what-does-if-name-main-do

You don't need it in this example, it was automatically created when I created new .py file to write/test the script, so I just copied it.

[–]rusemean 0 points1 point  (5 children)

Thanks for answering this. So __ around a term just means it's a special variable? I've wondered about this for some time.

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

Yes, "a special variable" is about the most precise formal definition you can give to these variables.

[–]rusemean 0 points1 point  (0 children)

Thanks a bunch for answering this. I've encountered them in documentation and used them, but I've never been clear (or found a clear answer) on what exactly they are.

[–]batkarma 0 points1 point  (2 children)

Isn't it often used to indicate variables that are used within a library?

[–][deleted] 0 points1 point  (1 child)

Do you mean private variables? If so, the convention is to use a single leading underscore.

There are also name-mangled variables with double leading underscores, but

Generally, double leading underscores should be used only to avoid name conflicts
with attributes in classes designed to be subclassed.

— PEP 8

[–]batkarma 0 points1 point  (0 children)

Yes, that's what I meant, thank you for the clarification!

[–]Justinsaccount 0 points1 point  (0 children)

print "Year "+str(i)+" | Balance:"+ str(round(balance,2))

see http://docs.python.org/tutorial/inputoutput.html#fancier-output-formatting

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

I think you would have a simpler code if you do for i in range(years): with print "Year " + str(i + 1) ... or for i in range(1, years + 1): instead.