Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Location
    U.S.A
    Posts
    129
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Python noob. simple function()

    I'm just kind of going through simple things to get a feel for how the syntax and programing in general go. This is my first language so I'm just trying to grasp the idea behind everything (I'm using python as a learning language).
    Can someone tell my what the problem with this simple code is?
    Code:
    def mult(x):
             if x<0:          
                 print "Your entered a number less than zero, please try again"
                 mult(int(raw_input("please enter a number above 1 to be multiplied")))
             else  if x==0:
                 print "Your answer is zero, please input a number above 1"
                 mult(int(raw_input("please enter a number above 1 to be multiplied")))
             else if x>1:
                 y=int(raw_input("Please enter a number to multipy it"))
                 print y * x
    ######Should there be a break somewhere, like here?
    mult(int(raw_input("please enter a number above 1 to be multiplied")))
    I have seen something about a break, and I remember breaks from php's switches, so I was wondering if I should use them in this? If so can someone please lead me to a real good beginners tut for python, or what I'm having the problem with. Every tut I go to seems like it's either simple x + y or it's to advanced for me to understand.


    Thanks,


    Joe

    P.S. Is there a way to allow you to see indentation?
    Last edited by awsomejoe23; 06-15-2007 at 07:52 PM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    First when posting python code or any other code you should really enclose it in VB's code or php tags so it retains it's formatting. I say that because python is indentation and whitespace sensitive so we can't tell if there is a problem with seeing how you have the code formatted.

    Second describe how it is suppose to work or what is not working or what errors you are encountering. You shouldn't make us guess what the problem is especially if we don't know what it is you are trying to accomplish.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Location
    U.S.A
    Posts
    129
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm trying to learn the sytax and how to make it work. The program is really simple.
    The user enters a number above 1 (x), then another number (y).
    I am trying to make it a little more complicated so I'm trying to make it so you have to make a number above 1, as well as using a function in it, because I'm trying to understand how the functions work. is that good enough.

    Thanks,



    Joe
    EDIT:
    I was mixing it up with php. It's elif not elseif or else if.

    Got it working.
    Last edited by awsomejoe23; 06-15-2007 at 08:46 PM.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You can use "elif" and "else if" in python. It is more likely you didn't have your indentation correct which is why I couldn't help you until I could see how you had it formatted.

    That is one of Python's downfalls. Instead of progressing forwards in evolution like most modern languages they chose to use indentation/whitespace to designate the end of blocks of code.

    That's just a very bad idea.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    Regular Coder croatiankid's Avatar
    Join Date
    Jan 2006
    Posts
    665
    Thanks
    1
    Thanked 12 Times in 12 Posts
    Just wanted to note that there's a seperate python forum (under "Other server side languages/issues"), so in the future you might get faster/more relevant responses.

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by awsomejoe23 View Post
    I'm just kind of going through simple things to get a feel for how the syntax and programing in general go. This is my first language so I'm just trying to grasp the idea behind everything (I'm using python as a learning language).
    Can someone tell my what the problem with this simple code is?
    Code:
    def mult(x):
             if x<0:          
                 print "Your entered a number less than zero, please try again"
                 mult(int(raw_input("please enter a number above 1 to be multiplied")))
             else  if x==0:
                 print "Your answer is zero, please input a number above 1"
                 mult(int(raw_input("please enter a number above 1 to be multiplied")))
             else if x>1:
                 y=int(raw_input("Please enter a number to multipy it"))
                 print y * x
    ######Should there be a break somewhere, like here?
    mult(int(raw_input("please enter a number above 1 to be multiplied")))
    I have seen something about a break, and I remember breaks from php's switches, so I was wondering if I should use them in this? If so can someone please lead me to a real good beginners tut for python, or what I'm having the problem with. Every tut I go to seems like it's either simple x + y or it's to advanced for me to understand.


    Thanks,


    Joe

    P.S. Is there a way to allow you to see indentation?
    use a while loop and break to a condition. the below code for illustration only.
    Code:
    while 1:
        x=raw_input("please enter a number above 1 to be multiplied,q to quit")
        if x == 'q':break
        if int(x)<0:    #usually a negative number is also valid.       
            print "Your entered a number less than zero, please try again"        
        elif int(x)==0:
            print "Your answer is zero, please input a number above 1"
        else:
            y=int(raw_input("Please enter a number to multipy it"))
            print y * x
    Last edited by ghostdog74; 06-16-2007 at 05:21 PM.

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Spookster View Post
    You can use "elif" and "else if" in python.
    no, you can't use "else if" in python. The correct syntax for "else if " in python is "elif"

    It is more likely you didn't have your indentation correct which is why I couldn't help you until I could see how you had it formatted.
    you should be able to figure out though....its very simple.

    That is one of Python's downfalls. Instead of progressing forwards in evolution like most modern languages they chose to use indentation/whitespace to designate the end of blocks of code.
    it is not a downfall like you said. Before you spout nonsense, you should substantiate why you think indentation/whitespace to designate end of blocks of code is no good. would you rather see open and close braces lying all over the place in your code, or nicely aligned white-spaced code. Python forces you to write legible and readable code.

    That's just a very bad idea.
    GVR doesn't think that way though

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by ghostdog74 View Post
    no, you can't use "else if" in python. The correct syntax for "else if " in python is "elif"


    you should be able to figure out though....its very simple.


    it is not a downfall like you said. Before you spout nonsense, you should substantiate why you think indentation/whitespace to designate end of blocks of code is no good. would you rather see open and close braces lying all over the place in your code, or nicely aligned white-spaced code. Python forces you to write legible and readable code.


    GVR doesn't think that way though

    Ok you are correct on the elif. I use too many languages at work.

    As for the indentation/whitespace being it's downfall that is not nonsense. It reduces readability of code drastically. It's more readable to have braces designating blocks of code. But that's okay. You can have your opinion and the rest of the world can have theirs.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Spookster View Post
    As for the indentation/whitespace being it's downfall that is not nonsense. It reduces readability of code drastically. It's more readable to have braces designating blocks of code. But that's okay. You can have your opinion and the rest of the world can have theirs.
    how long have you programmed python? if you have programmed python long enough, you would have discovered the beauty of whitespace. braces are considered "noises" and it makes code unreadable and hard to troubleshoot, especially if you have to read code written by inexperienced programmers and it has many thousands of lines of code.

    Code:
    if ( x==1 ) {
      do_something()
    } 
    else if ( x==2 ) {
        do_something()
    }
    else {
        exit
    }
    vs this
    Code:
    if x==1:
       do_something()
    elif x==2:
       do_something()
    else:
       exit
    you don't read a piece of english essay that contains a lot of braces do you?

  • #10
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ghostdog74 View Post
    how long have you programmed python? if you have programmed python long enough, you would have discovered the beauty of whitespace. braces are considered "noises" and it makes code unreadable and hard to troubleshoot, especially if you have to read code written by inexperienced programmers and it has many thousands of lines of code.
    Myself, 4 years of Python about 8-9 of C/C++. Of course I program in a multitude of languages - including my own scripting language developed for my game engine. I find that braces add much more "physical" logical structure and presentation to the code. I'd even say the whitespace is what I dislike most about Python - which is why I opted to create my own for my games instead of just plugging in Python (didn't like LUA either really).

    you don't read a piece of english essay that contains a lot of braces do you?
    Dude, this is mathematics not the arts. You can't be comparing programming syntax and semantics to written syntax and semantics of a spoken language like that. It's just, wrong.

    Then again it's also acceptable (especially in exams) to denote paragraphs by surrounding them in square ([]) brackets - which even though I still say you can't compare does prove you wrong, particularly when it comes to an essay.
    Last edited by Mhtml; 06-17-2007 at 11:45 AM.
    Omnis mico antequam dominus Spookster!

  • #11
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Mhtml View Post
    Myself, 4 years of Python about 8-9 of C/C++. Of course I program in a multitude of languages - including my own scripting language developed for my game engine. I find that braces add much more "physical" logical structure and presentation to the code. I'd even say the whitespace is what I dislike most about Python - which is why I opted to create my own for my games instead of just plugging in Python (didn't like LUA either really).
    i am not here to change anybody's mind or preferences. Just because you advocate braces doesn't mean everybody does. Many people are using Python, so baseless comments like whitespace is a downfall blah blah is certainly not warranted. It gives people wrong perception.

    Dude, this is mathematics not the arts. You can't be comparing programming syntax and semantics to written syntax and semantics of a spoken language like that. It's just, wrong.
    dude, you don't get what i mean. I am talking about readability of code. Not mathematics.
    Last edited by ghostdog74; 06-17-2007 at 12:18 PM.

  • #12
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by ghostdog74 View Post
    baseless comments like whitespace is a downfall blah blah is certainly not warranted. It gives people wrong perception.

    And claiming I am spouting nonsense was warranted?

    You can have your opinion, that's fine. Speaking from experience though you will generally find that most programmers prefer to have distinct markers for blocks of code.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #13
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Spookster View Post
    And claiming I am spouting nonsense was warranted?
    why not? by definition, nonsense means not the truth. Python is being used by many people, even Google, so its not the truth by saying "whitepspace is a downfall" of Python. Please check the meaning of downfall. Google has not fallen, has it? You could have used a better word or sentence.

    You can have your opinion, that's fine. Speaking from experience though you will generally find that most programmers prefer to have distinct markers for blocks of code.
    Again, this is based on your experience,in the perspective of a programmer that is already very accustomed to program in languages that use extensive markers blah blah..if a new programmer has been exposed to readable languages like Python from the start, do you think they would still prefer braces?. Also, you are not putting on your shoes for the readers of the code, especially when the code does not belong to the programmer after project completion. The reader will have a hard time if the code is not properly written. At least if without braces and extraneous stuffs like that, it would be easier. like reading an essay...

  • #14
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by ghostdog74 View Post
    why not? by definition, nonsense means not the truth. Python is being used by many people, even Google, so its not the truth by saying "whitepspace is a downfall" of Python. Please check the meaning of downfall. Google has not fallen, has it? You could have used a better word or sentence.



    Again, this is based on your experience,in the perspective of a programmer that is already very accustomed to program in languages that use extensive markers blah blah..if a new programmer has been exposed to readable languages like Python from the start, do you think they would still prefer braces?. Also, you are not putting on your shoes for the readers of the code, especially when the code does not belong to the programmer after project completion. The reader will have a hard time if the code is not properly written. At least if without braces and extraneous stuffs like that, it would be easier. like reading an essay...
    Ok you're right and everybody else is wrong. How silly of us all to think using whitespace as desgination of code blocks was bad. We all now see the light. End of argument.
    Last edited by Spookster; 06-18-2007 at 03:39 AM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #15
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    I appreciate the whitespace in Python... I just internally map whitespace to mean "block structure" when I'm writing in it, whereas when I'm doing Javascript, I map { and } to indicate the same block structure. One should always be properly indenting code anyway (except for the rare instance here and there), so the {} are just extra characters.

    Besides, everybody knows that the perfect language is Scheme, which has no place for silly {} or mandatory whitespace.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •