...

View Full Version : Python noob. simple function()



awsomejoe23
06-15-2007, 06:32 PM
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?


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?

Spookster
06-15-2007, 06:49 PM
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.

awsomejoe23
06-15-2007, 06:54 PM
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.

Spookster
06-15-2007, 09:11 PM
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.

croatiankid
06-16-2007, 03:06 PM
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.

ghostdog74
06-16-2007, 04:13 PM
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?


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.


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

ghostdog74
06-16-2007, 04:20 PM
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

Spookster
06-16-2007, 10:41 PM
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.

ghostdog74
06-17-2007, 02:50 AM
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.



if ( x==1 ) {
do_something()
}
else if ( x==2 ) {
do_something()
}
else {
exit
}

vs this


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?

Mhtml
06-17-2007, 10:36 AM
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.

ghostdog74
06-17-2007, 11:13 AM
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.

Spookster
06-17-2007, 06:43 PM
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.

ghostdog74
06-18-2007, 01:33 AM
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...

Spookster
06-18-2007, 02:37 AM
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. :thumbsup:

jkd
06-18-2007, 06:31 AM
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. ;)

jkd
06-18-2007, 06:32 AM
use a while loop and break to a condition.

But the original recursion was so much prettier. :)

Mhtml
06-18-2007, 10:18 AM
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.Since when is Google the epitome of programming practise and paradigms?


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...Well there is a perfect solution there, don't write garbage code and use efficient internal and external documentation. You have a point, Python is good for teaching programming fundamentals because the syntax isn't particularly heavy - but of course once you understand it you can then move on and play with the big boys. (yes I'm deliberately attempting to stir people up - it's in my nature).


dude, you don't get what i mean. I am talking about readability of code. Not mathematics.So am I?

I've never said Python is a bad language - it has some use for lightweight applications where an interpreted language is preferred.

ghostdog74
06-18-2007, 12:20 PM
But the original recursion was so much prettier. :)

that's where things get interesting. What happens when the user continuously enters numbers less than 0 or equal to 0 ? before the function can return, its being called again. the stack gets built up blah blah...you should know where i am coming from. I am not saying using recursion is no good, but i am advocating using it for the right purpose and at the right time. such as the famous fibonacci blah blah...

ghostdog74
06-18-2007, 12:26 PM
Since when is Google the epitome of programming practise and paradigms?

i am using Google as an example, because somehow, somewhere, part of google is coded in Python. Since google can use Python, so saying "whitespace is downfall" of Python is not accurate. Please read the posts carefully.



Well there is a perfect solution there, don't write garbage code and use efficient internal and external documentation.

can you enforce this perfect solution in every programmer, old and new?

jkd
06-18-2007, 01:28 PM
that's where things get interesting. What happens when the user continuously enters numbers less than 0 or equal to 0 ?


The same thing that happens with your looping construct. ;)

Except, because Python is not tail-recursive, after several thousand/million tries, the runtime stack gets too large and the program exits. The only user that would do that is bot, in which case there is an obvious bug in the bot, and perhaps exiting early instead of an infinite loop might be desirable.


I am not saying using recursion is no good, but i am advocating using it for the right purpose and at the right time. such as the famous fibonacci blah blah...

Fibonnaci numbers are a trivial example of recursion, and actually a bad one unless you pass an accumulator along to optimize it tail recursively. I'm a fan of recursion everywhere, as it tends to simplify algorithms and make code more consistent (either it works, or it hits an infinite loop a null-pointer immediately; no half-baked sorta-working things you often see otherwise).

ghostdog74
06-18-2007, 02:14 PM
The same thing that happens with your looping construct. ;)

well, i may or may not have much programming experience as you, but i do know a loop construct and recursive construct are different.


while 1:
# ask for user input
# if equal q , break
# do something and go back to beginning of loop.

can you explain how the above while loop is as recursive as this below:


def somefunc():
#ask for user input (assuming it always does not satisfy a condition)
# if doesn't satisfy, execute somefunc() again and again and again....




Fibonnaci numbers are a trivial example of recursion, and actually a bad one unless you pass an accumulator along to optimize it tail recursively. I'm a fan of recursion everywhere, as it tends to simplify algorithms and make code more consistent .
well it may be a trivial, but because of its mathematical nature, a recursive method is definitely appropriate for doing fibo. also because usually generating fibo doesn't really need much user interaction (or does it?) ... you just code it, ensure that it returns properly, ask the user for an input (or not) , and then pooof..you are off the generate them...

BUT not for a user menu like OP posted....you will not know what the user might do. Like entering -1 or 0 (in the example OP posted) continuously.


(either it works, or it hits an infinite loop a null-pointer immediately; no half-baked sorta-working things you often see otherwise)

yup, you said it, if not careful, it can hit infinite loops blah blah..that's why i say, use recursion with caution and at appropriate situations. $0.02

jkd
06-18-2007, 04:39 PM
They do the same thing, just in different ways. In a compiled language with tail-optimization, it's actually quite possible that they would compile to the same binary executable.

It is also often simpler to think about things recursively. The op's program was designed basically:

1. Try getting user input.
2. If bad, try again.
3. Else, multiply the numbers together.

What the difference between the iterative and recursive versions is simply lies within your interpretation of "try again". I like to think of try again as executing the calling function again, while you might prefer to iterate a loop.

ghostdog74
06-19-2007, 12:11 AM
They do the same thing, just in different ways. In a compiled language with tail-optimization, it's actually quite possible that they would compile to the same binary executable.

It is also often simpler to think about things recursively. The op's program was designed basically:

1. Try getting user input.
2. If bad, try again.
3. Else, multiply the numbers together.

What the difference between the iterative and recursive versions is simply lies within your interpretation of "try again". I like to think of try again as executing the calling function again, while you might prefer to iterate a loop.

this is where i get confused. does each iteration of the Python loop put something on to the stack , like a return address ? From what i know, doesn't calling a function recursively put the return addresses of the function(that has not returned to the caller )on the stack, again and again?

jkd
06-19-2007, 03:39 AM
From what i know, doesn't calling a function recursively put the return addresses of the function(that has not returned to the caller )on the stack, again and again?

This is why I kept mentioning tail-recursive calls. A smart interpreter or compiler can recognize when the stack frame can be replaced entirely by the next one, and replaces it instead of pushing another one on.

Mhtml
06-20-2007, 05:55 PM
i am using Google as an example, because somehow, somewhere, part of google is coded in Python. Since google can use Python, so saying "whitespace is downfall" of Python is not accurate. Please read the posts carefully.I'm quite sure that's not even how Spookster intended you to take what he said - text in context man.


can you enforce this perfect solution in every programmer, old and new?No, but I don't have to. I don't maintain garbage, I destroy it and replace it with decent code. And I refuse to work with dodgy coders.

Spookster
06-20-2007, 06:38 PM
I'm quite sure that's not even how Spookster intended you to take what he said - text in context man.



You are correct sir. :thumbsup: I find it is pointless to continue arguing with someone when they begin taking things out of context and picking apart which word(s) you used. I should know, I used to do that. I also got the impression I was arguing with someone with far less programming experience than I who had been taught that in school so once again pointless to argue when that's what their instructor taught them. They'll figure things out on their own eventually with experience hopefully.

ghostdog74
06-21-2007, 02:16 AM
I'm quite sure that's not even how Spookster intended you to take what he said - text in context man.

so that's an area he need to learn and do well right? use proper words and think properly before one writes. Because there are many kinds of audiences browsing this forum.



No, but I don't have to. I don't maintain garbage, I destroy it and replace it with decent code. And I refuse to work with dodgy coders.
so you couldn't enforce it , so many people will still write garbage (all over the world), and that's something you and me couldn't do, am i right to say that?. This brings back the point that i am advocating, IF they use a language that reduces "noises", those "garbage code" will be easier to read and troubleshoot if things happen..at least.

ghostdog74
06-21-2007, 02:52 AM
You are correct sir. :thumbsup: I find it is pointless to continue arguing with someone when they begin taking things out of context and picking apart which word(s) you used.
dude, i know what you are talking about from the start. But please do brush up your english and use proper choice of words next time you write "sensitive" stuffs like this, especially when it comes to talking about languages. If you don't, then expect people to bombard you back. That happens everywhere.
Let me quote you again:


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.

With sentences like this, you can expect people to bombard you with:
""" The part about evolution blah blah..where did you get that idea from? And why is using whitespace/indentation backwards in evolution. Do you have any facts that substantiate what you say. """
So what are you going to tell them? All these, you should think properly before posting.

Spookster
06-21-2007, 03:31 AM
But please do brush up your english and use proper choice of words next time you write "sensitive" stuffs like this, especially when it comes to talking about languages.

ROFLOL I'll try to brush up on my English stuffs dude. Maybe you could teach me how to use proper grammar. Maybe someday with much practice I can write the English language as well as you.

ghostdog74
06-21-2007, 04:13 AM
an example from another thread i was reading:




Quote:
Originally Posted by it career
It depends on what kind of project you will be working, for distributive stuffs java is far better than C++
I mean, truly - since when?

see how it career posted. he mentioned java is far better than c++ for distributive stuffs without elaborating more, where did he get his facts? If they are purely just his opinions, then he should state it down. If not, give proof, otherwise he will mislead the OP. And this leads to my assumption that Mhtml doesn't think likewise (bomb me if i am wrong k) because of the way he replied. There is a tinge of disagreement. Moral of the story on the whole..write carefully, think before you write, substantiate your facts...blah blah..i think you have got it by now.

With this, lets end this will a big hand shake okay. I am not here to argue until the cow comes home, or teach english to anyone too..:)

mentalhorse
06-22-2007, 03:19 AM
You need to learn to type better ghostdog74. Also stop using "blah blah...": I can't understand what you mean the "blah blah..."ing to mean. I might misinterpret it. ;)

ghostdog74
06-22-2007, 07:17 AM
You need to learn to type better ghostdog74. Also stop using "blah blah...": I can't understand what you mean the "blah blah..."ing to mean. I might misinterpret it. ;)
same to you. as for the blah blah, if you don't understand "blah blah", then substitute it with "etc". I am sure you know what "etc" means. look, you don't need to purposely stir this thread up again. If you are too bored, then go read a book.

mentalhorse
06-23-2007, 01:08 AM
Stop telling people to use better english when you can't type worth ****. Maybe I will go read a book. Maybe you should too.

ghostdog74
06-23-2007, 03:28 AM
Stop telling people to use better english when you can't type worth ****. Maybe I will go read a book. Maybe you should too.

kid, you better return to school and study more. Everybody can type, but not everybody can write. There's a difference. This shall be my last post here.

mentalhorse
06-23-2007, 07:03 PM
Your lack of good grammar makes you look like a 12 year old. It reflects on your 'writing' 'skill'. And not everybody can type. Look at yourself.

jkd
06-24-2007, 10:34 PM
Enough.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum