
-----------------------------------
Sniper4Life
Wed Apr 08, 2009 7:28 pm

My First Program!
-----------------------------------
This is my first Python script. I started Python yesterday and I just wanted the CompSci community to see it and evaluate it. The general idea of the calculator is thought of from another script written by someone else NOT me. I just added in a couple of more functions to the script, and it was originally started from scratch. Its not long or anything but I'm still somewhat proud of it :D. Please do tell me better ways I could have made this script(shorter...more efficient), and remember, criticism is encouraged!

-----------------------------------
Sniper4Life
Wed Apr 08, 2009 7:29 pm

Re: My First Program!
-----------------------------------
No need to download...here is the code!


def options():

    print "Welcome to Abrar's Program!"
    print " "
    print "1) Add numbers"
    print "2) Subtract numbers"
    print "3) Multiply a number"
    print "4) Divide a number"
    print "5) Use Exponents"
    print "6) Quit Program"
    print " "

    return input ("What would you like to do?")

def add (a,b):
    print a,"+",b,"=", a+b

def sub (a,b):
    print b,"-",a,"=",b-a

def mult (a,b):
    print a, "*" , b , "=", a*b

def div (a,b):
    print a,"/",b,"=", a/b
    
def sqaure (a,b):
    print a," ", "powered by", " ", b,"=", a**b




loop=1
function=0
while loop==1:
    function=options()

    if function==1:
        add(input("The number: "), input("plus: "))

    elif function==2:
        sub(input("This number: "), input("subtracted from this number: "))

    elif function==3:
        mult (input("First number: "), input("second number: "))

    elif function==4:
        div  (input("This number: "), input ("divided by this number: "))
        

    elif function==5:
        sqaure(input("The base: "), input("the exponent: "))

    elif function==6:
        loop = 0

        print "See you next time on Abrar's Calculator!"


Mod Edit: Remember to use syntax tags! Thanks :) Code Here

-----------------------------------
saltpro15
Wed Apr 08, 2009 7:53 pm

RE:My First Program!
-----------------------------------
nice, looks familiar though ;)
I'm thinking of writing a new game with pygame, maybe if it's any good i'll post it

-----------------------------------
Sniper4Life
Wed Apr 08, 2009 8:03 pm

Re: My First Program!
-----------------------------------
lol yes very familiar  :wink: 
i didnt copy and paste atleast :D
i remembered what i read :P
then learned to do it on my own :D
im gonna add more stuff to it later on?
like getting the square root...finding the circumference of a cirlce...radius of a circle...stuff like that
basic...easy...
anyways i could improve on it?

-----------------------------------
saltpro15
Wed Apr 08, 2009 8:05 pm

RE:My First Program!
-----------------------------------
well, the suggestions you made would be excellent, I would also recommend taking advantages of python's classes, which are very handy

-----------------------------------
Sniper4Life
Wed Apr 08, 2009 8:09 pm

Re: My First Program!
-----------------------------------
u mean actually taking python classes?
or the online resources available...???

and python is SOOO much easier than c++ :D

man i was kind dumb to start with c++ :P

btw salt...what age did you start coding?
and having an interest in computers
i asked wtd before but he nvr answered me :(

-----------------------------------
saltpro15
Wed Apr 08, 2009 8:33 pm

RE:My First Program!
-----------------------------------
no, I meant the class function in python, maybe wait a bit to get into that, it's sort of advanced.  I started coding in October, I am currently programming in Turing,C++ and Python, I've always been interested in comp's though

EDIT
I'm 15, in Grade 10

-----------------------------------
Sniper4Life
Wed Apr 08, 2009 9:12 pm

RE:My First Program!
-----------------------------------
ok good to see you started at age 15 :D
im 13 and i started in january :D

i thought i was sort of "late" to all possible good programmers because i have this one friend and he is PRO with programming
he's been writing scripts since he was 7 or 8 i think?
so ya....i thought i was like a complete noob :P
good to see i didnt start that late :P

-----------------------------------
Analysis Mode
Wed Apr 08, 2009 10:13 pm

Re: My First Program!
-----------------------------------
Not bad.  Python's a pretty good language.

You shold also try Pascal, always a good language to start with, easy to read, etc.

C++ should be your second or third language (it's my second), much more powerful, STL's, etc.  you should know the basics of programming (variables, procedures/functions, loops, etc.) before moving onto C++.  Knowing PAscal made it much easier to learn c++ in my case.

-----------------------------------
wtd
Wed Apr 08, 2009 11:54 pm

RE:My First Program!
-----------------------------------
Replace the test in the loop.  You can more directly express this in the following form.

[code]while True:
    if blah:
        ...
        break[/code]

Also, your conditional has some other issues.

A) The input function returns a string.  You check for equality against integers.  What do you expect if I input 4 as my choice of options?  Now, what actually happens?

B) Your conditional is not robust.  What happens if I choose option 13?  What should happen?

-----------------------------------
Sniper4Life
Thu Apr 09, 2009 6:21 am

Re: My First Program!
-----------------------------------
hehe wtd i noticed the thing you said too :D
the option 13 thing :D
see...problem is...i DONT KNOW how to do that efficiently...so THERES my problem...so could you tell me how i could do that?

and the  

while True:
 if blah
     ***
     break


what do you mean with it?

-----------------------------------
DemonWasp
Thu Apr 09, 2009 10:01 am

RE:My First Program!
-----------------------------------
I'll explain:

[code]
while true  // this means "loop forever, until I say break"
    if blah  // test for some condition. You have lots of these above; in particular, when it says function==6, you're setting "loop" to zero, meaning "stop looping"
        break;  // this means "stop looping right now"
[/code]

Side note: How long before someone finally stamps a "Not for first-time coders" label on C++? That language is probably just about the worst thing for a new programmer - worse than Windows, worse than a bad teacher. It is the graveyard of new programmers.

-----------------------------------
Sniper4Life
Thu Apr 09, 2009 11:38 am

Re: RE:My First Program!
-----------------------------------
Replace the test in the loop.  You can more directly express this in the following form.



B) Your conditional is not robust.  What happens if I choose option 13?  What should happen?

k first...could anyone explain to me an EFFICIENT way of making any number over 6 do something specific?
using a loop or sumthing?

and btw demonwasp it doesnt really help improve the performance of the program...just the code itself?

-----------------------------------
[Gandalf]
Thu Apr 09, 2009 12:01 pm

RE:My First Program!
-----------------------------------
[code]elif function > 6:[/code]

-----------------------------------
saltpro15
Thu Apr 09, 2009 2:36 pm

Re: RE:My First Program!
-----------------------------------
ok good to see you started at age 15 :D
im 13 and i started in january :D

i thought i was sort of "late" to all possible good programmers because i have this one friend and he is PRO with programming
he's been writing scripts since he was 7 or 8 i think?
so ya....i thought i was like a complete noob :P
good to see i didnt start that late :P

you can really start whenever. It all depends on how well you take to programming.  will you be in high school next year? if so, I recommend signing up for  [url=www.dwite.org]DWITE  it's an excellent way to get experience in programming contests, Dan and Tony host it monthly October-February.

-----------------------------------
wtd
Thu Apr 09, 2009 4:32 pm

RE:My First Program!
-----------------------------------
I started programming when I was 18, if memory serves.

-----------------------------------
Sniper4Life
Fri Apr 10, 2009 8:41 am

RE:My First Program!
-----------------------------------
ya i kno you can really start whenever...it all depends on how much time you put into the subject and how easily you understand it....im just glad i got sort of a "head start" compared to saltpro and wtd
