Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 My First Program!
Index -> Programming, Python -> Python Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Sniper4Life




PostPosted: Wed Apr 08, 2009 7:28 pm   Post subject: 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 Very Happy. Please do tell me better ways I could have made this script(shorter...more efficient), and remember, criticism is encouraged!
Sponsor
Sponsor
Sponsor
sponsor
Sniper4Life




PostPosted: Wed Apr 08, 2009 7:29 pm   Post subject: Re: My First Program!

No need to download...here is the code!


Python:
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 Smile
code:
[syntax="python"]Code Here[/syntax]
saltpro15




PostPosted: Wed Apr 08, 2009 7:53 pm   Post subject: RE:My First Program!

nice, looks familiar though Wink
I'm thinking of writing a new game with pygame, maybe if it's any good i'll post it
Sniper4Life




PostPosted: Wed Apr 08, 2009 8:03 pm   Post subject: Re: My First Program!

lol yes very familiar Wink
i didnt copy and paste atleast Very Happy
i remembered what i read Razz
then learned to do it on my own Very Happy
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




PostPosted: Wed Apr 08, 2009 8:05 pm   Post subject: 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




PostPosted: Wed Apr 08, 2009 8:09 pm   Post subject: Re: My First Program!

u mean actually taking python classes?
or the online resources available...???

and python is SOOO much easier than c++ Very Happy

man i was kind dumb to start with c++ Razz

btw salt...what age did you start coding?
and having an interest in computers
i asked wtd before but he nvr answered me Sad
saltpro15




PostPosted: Wed Apr 08, 2009 8:33 pm   Post subject: 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




PostPosted: Wed Apr 08, 2009 9:12 pm   Post subject: RE:My First Program!

ok good to see you started at age 15 Very Happy
im 13 and i started in january Very Happy

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 Razz
good to see i didnt start that late Razz
Sponsor
Sponsor
Sponsor
sponsor
Analysis Mode




PostPosted: Wed Apr 08, 2009 10:13 pm   Post subject: 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




PostPosted: Wed Apr 08, 2009 11:54 pm   Post subject: 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


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




PostPosted: Thu Apr 09, 2009 6:21 am   Post subject: Re: My First Program!

hehe wtd i noticed the thing you said too Very Happy
the option 13 thing Very Happy
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
code:

while True:
 if blah
     ***
     break


what do you mean with it?
DemonWasp




PostPosted: Thu Apr 09, 2009 10:01 am   Post subject: 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"


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




PostPosted: Thu Apr 09, 2009 11:38 am   Post subject: Re: RE:My First Program!

wtd @ Wed Apr 08, 2009 10:54 pm wrote:
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]




PostPosted: Thu Apr 09, 2009 12:01 pm   Post subject: RE:My First Program!

code:
elif function > 6:
saltpro15




PostPosted: Thu Apr 09, 2009 2:36 pm   Post subject: Re: RE:My First Program!

Sniper4Life @ Wed Apr 08, 2009 wrote:
ok good to see you started at age 15 Very Happy
im 13 and i started in january Very Happy

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 Razz
good to see i didnt start that late Razz


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 DWITE it's an excellent way to get experience in programming contests, Dan and Tony host it monthly October-February.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: