Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Else statement executing when it shouldnt

Reply
Thread Tools

Else statement executing when it shouldnt

 
 
eli m
Guest
Posts: n/a
 
      01-21-2013

>
>
>
> Your else is lined up with while, not with if.
>
>
>
> -m
>
>
>
>
>
> --
>
> Lark's Tongue Guide to Python: http://lightbird.net/larks/
>
>
>
> When a friend succeeds, I die a little. Gore Vidal

Its lined up. It got messed up when i copied the code into the post.

 
Reply With Quote
 
 
 
 
René Klačan
Guest
Posts: n/a
 
      01-21-2013
Examples:

# else branch will be executed
i = 0
while i < 5:
i += 1
else:
print('loop is over')


# else branch will be executed
i = 0
while i < 5:
i += 1
if i == 7:
print('i == 7')
break
else:
print('loop is over')


# else branch wont be executed
i = 0
while i < 5:
i += 1
if i == 3:
print('i == 3')
break
else:
print('loop is over')

On Mon, Jan 21, 2013 at 5:57 AM, eli m <(E-Mail Removed)> wrote:

> On Sunday, January 20, 2013 8:54:13 PM UTC-8, René Klačan wrote:
> > You have to break while loop not to execute else branch
> >
> >
> > Rene
> >
> >
> >

> Can you explain in more detail please.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
 
 
 
René Klačan
Guest
Posts: n/a
 
      01-21-2013
Examples:

# else branch will be executed
i = 0
while i < 5:
i += 1
else:
print('loop is over')


# else branch will be executed
i = 0
while i < 5:
i += 1
if i == 7:
print('i == 7')
break
else:
print('loop is over')


# else branch wont be executed
i = 0
while i < 5:
i += 1
if i == 3:
print('i == 3')
break
else:
print('loop is over')

On Mon, Jan 21, 2013 at 5:57 AM, eli m <(E-Mail Removed)> wrote:

> On Sunday, January 20, 2013 8:54:13 PM UTC-8, René Klačan wrote:
> > You have to break while loop not to execute else branch
> >
> >
> > Rene
> >
> >
> >

> Can you explain in more detail please.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      01-21-2013
On Jan 21, 2:59*pm, eli m <(E-Mail Removed)> wrote:
> Its lined up. It got messed up when i copied the code into the post.


Sorry, we're not going to take your word for it. Reduce it to the
minimal amount of code that reproduces your error and post that.
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      01-21-2013
On Jan 21, 2:40*pm, eli m <(E-Mail Removed)> wrote:
> an else statement is running when it shouldnt be. It is
> on the last line. Whenever i am in the math or game
> function, when i type in main, it goes back to the start
> of the program, but it also says not a valid function.
> I am stumped!


Here is your code with the irrelevancy stripped away:

function = raw_input("Type in a function:")
#start math loop
if function == "math":
#math code
if function == "random number":
#random code
if function == "games":
#games code
if function == "help":
#help code
else:
print ("Not a valid function")

Say you enter 'math'. It passes the first condition, so runs the math
code.
It then fails on the next 3 conditions, the last of which has an else,
so if you type _anything_ other than 'help', you'll see "Not a valid
function".

Easy answer, use `elif` ("else if") instead of else for the subsequent
tests:

if function == "math":
#math code
elif function == "random number":
#random code
elif function == "games":
#games code
elif function == "help":
#help code
else:
print ("Not a valid function")

Better answer: read up on real functions, and look into dictionary
dispatch:

def f_math():
#math code

def f_random_number():
#random code

<etc>

function_dispatcher = {
'math': f_math,
'random number': f_random_number,
<etc>
}

while cmd == 0:
function_name = raw_input("Type in a function:")
if function_name in function_dispatcher:
function_dispatcher[function_name]()
else:
print("Not a valid function")

To have your functions break out of the loop, use a `global` variable
or pass a context object into each function to allow them to set
`cmd`.
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      01-21-2013
On Jan 21, 2:54*pm, eli m <(E-Mail Removed)> wrote:
> hint: Use the comments in the code to find out where my error is.


Pro-tip: when people you're asking for help tell you how you can make
it easier for them to help you, a snide response isn't the correct
approach.
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      01-21-2013
On 01/20/2013 11:59 PM, eli m wrote:
>
>>
>>
>>
>> Your else is lined up with while, not with if.
>>
>>
>>
>> -m
>>
>>
>>
>>
>>
>> --
>>
>> Lark's Tongue Guide to Python: http://lightbird.net/larks/
>>
>>
>>
>> When a friend succeeds, I die a little. Gore Vidal

> Its lined up. It got messed up when i copied the code into the post.
>


I would recommend using while True: and break vs. while var: as you
have. In most cases while True: works better, especially in case of long
and/or nested 'while' loops, as you have.

'while True' blocks have two advantages: 1. you can break the loop at
any location and 2. when looking at the code, you can tell on which
condition it breaks by looking at the break line.

Even more importantly, break it up into a few functions. The code as you
have it is too hard to work with and to debug.

It's hard to tell what your 'else' is lined up to, or whether some other
lines are mis-aligned, as well.

Generally, try to avoid making a loop if it's 20+ lines; if there are
nested loops, it makes things even worse. Compare:

if something:
while True:
if not process(): break

def process():
[... 20 lines that loop ...]
[ return None to break the loop ]

Now this is really clear, because just by looking at the first three
lines, I know what the loop is supposed to do (process something), that
it continues looping until it returns a false value; when looking at
the function body I don't need to care which block it aligns to, I
already know the entire function body is in the while loop.

HTH, -m



--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

The irrational in the human has something about it altogether repulsive and
terrible, as we see in the maniac, the miser, the drunkard or the ape.
George Santayana

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-21-2013
On Sun, 20 Jan 2013 22:00:10 -0800, alex23 wrote:

> On Jan 21, 2:54*pm, eli m <(E-Mail Removed)> wrote:
>> hint: Use the comments in the code to find out where my error is.

>
> Pro-tip: when people you're asking for help tell you how you can make it
> easier for them to help you, a snide response isn't the correct
> approach.


Alex, thank you for saying this. I can now delete my *much* less polite
version saying the same thing.




--
Steven
 
Reply With Quote
 
eli m
Guest
Posts: n/a
 
      01-21-2013
On Sunday, January 20, 2013 9:56:59 PM UTC-8, alex23 wrote:
> On Jan 21, 2:40*pm, eli m <(E-Mail Removed)> wrote:
>
> > an else statement is running when it shouldnt be. It is

>
> > on the last line. Whenever i am in the math or game

>
> > function, when i type in main, it goes back to the start

>
> > of the program, but it also says not a valid function.

>
> > I am stumped!

>
>
>
> Here is your code with the irrelevancy stripped away:
>
>
>
> function = raw_input("Type in a function:")
>
> #start math loop
>
> if function == "math":
>
> #math code
>
> if function == "random number":
>
> #random code
>
> if function == "games":
>
> #games code
>
> if function == "help":
>
> #help code
>
> else:
>
> print ("Not a valid function")
>
>
>
> Say you enter 'math'. It passes the first condition, so runs the math
>
> code.
>
> It then fails on the next 3 conditions, the last of which has an else,
>
> so if you type _anything_ other than 'help', you'll see "Not a valid
>
> function".
>
>
>
> Easy answer, use `elif` ("else if") instead of else for the subsequent
>
> tests:
>
>
>
> if function == "math":
>
> #math code
>
> elif function == "random number":
>
> #random code
>
> elif function == "games":
>
> #games code
>
> elif function == "help":
>
> #help code
>
> else:
>
> print ("Not a valid function")
>
>
>
> Better answer: read up on real functions, and look into dictionary
>
> dispatch:
>
>
>
> def f_math():
>
> #math code
>
>
>
> def f_random_number():
>
> #random code
>
>
>
> <etc>
>
>
>
> function_dispatcher = {
>
> 'math': f_math,
>
> 'random number': f_random_number,
>
> <etc>
>
> }
>
>
>
> while cmd == 0:
>
> function_name = raw_input("Type in a function:")
>
> if function_name in function_dispatcher:
>
> function_dispatcher[function_name]()
>
> else:
>
> print("Not a valid function")
>
>
>
> To have your functions break out of the loop, use a `global` variable
>
> or pass a context object into each function to allow them to set
>
> `cmd`.


Thank you, that solved my problem. Sorry for my posts, i am a noob and thisis my first time posting on here.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      01-21-2013
On Tue, Jan 22, 2013 at 2:37 AM, eli m <(E-Mail Removed)> wrote:
> Thank you, that solved my problem. Sorry for my posts, i am a noob and this is my first time posting on here.


There's nothing wrong with being a noob, we all start out that way.
Want to be one of the people we love to help? Here are some tips:

http://www.catb.org/esr/faqs/smart-questions.html

It's longish, but you'll find it helpful. The principles laid out in
that document govern pretty much every geeky forum, and a good number
of others besides.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null Argument shouldnt be? A. Anderson C++ 8 06-28-2007 09:03 PM
Why shouldnt I get Vista (or which version) Nik Coughlin NZ Computing 27 02-09-2007 12:37 AM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
shouldnt this evaluate in a scalar context??? dutone Perl Misc 8 07-02-2004 04:46 PM
reroute to another url, but the first url must shouldnt change in theadress-mask guenther schoebel HTML 2 10-10-2003 07:44 AM



Advertisments