Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52171 > unrolled thread

Issues with if and elif statements in 3.3

Started bykrismesenbrink@gmail.com
First post2013-08-08 00:19 -0700
Last post2013-08-08 19:31 -0400
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Issues with if and elif statements in 3.3 krismesenbrink@gmail.com - 2013-08-08 00:19 -0700
    Re: Issues with if and elif statements in 3.3 Dave Angel <davea@davea.name> - 2013-08-08 10:34 +0000
      Re: Issues with if and elif statements in 3.3 Kris Mesenbrink <krismesenbrink@gmail.com> - 2013-08-08 15:20 -0700
        Re: Issues with if and elif statements in 3.3 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-08 19:37 -0400
    Re: Issues with if and elif statements in 3.3 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-08-08 19:31 -0400

#52171 — Issues with if and elif statements in 3.3

Fromkrismesenbrink@gmail.com
Date2013-08-08 00:19 -0700
SubjectIssues with if and elif statements in 3.3
Message-ID<02128546-7a5d-4fe8-a472-47cfd4f76342@googlegroups.com>
def town():
    print ("You stand in the middle of Coffeington while you descide what"
    " to do next, you have herd rumor of the Coffeington Caves that run"
    "under the city, would you like to check them out?")
    answer = input()
    if answer == ("yes") or ("Yes") or ("y"):
        print("You set out for the Coffeington Caves")
    elif answer == ("no") or ("No") or ("n"):
        print("Oh...well im sure you can find something else to do")
    else:
        print("You just stand  there")
town()



i don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end? i think anway.

[toc] | [next] | [standalone]


#52185

FromDave Angel <davea@davea.name>
Date2013-08-08 10:34 +0000
Message-ID<mailman.346.1375958120.1251.python-list@python.org>
In reply to#52171
krismesenbrink@gmail.com wrote:

> def town():
>     print ("You stand in the middle of Coffeington while you descide what"
>     " to do next, you have herd rumor of the Coffeington Caves that run"
>     "under the city, would you like to check them out?")
>     answer = input()
>     if answer == ("yes") or ("Yes") or ("y"):

This doesn't do what you think it does.  First it compares answer to
"yes".  Then it takes the result of that and OR's it with "Yes".  Then
it takes the result of that and OR's it with "y".  Finally it takes the
bool of the result and decides whether to execute the if-body.  Since
those OR's will always be true, it always executes the if-body, and
never the elif or else body.

Fix the expression to what you presumably meant:

if answer == "yes" or answer == "Yes" or answer == "y":

Or less typing:

if answer in ("yes", "Yes", "y"):




>         print("You set out for the Coffeington Caves")
>     elif answer == ("no") or ("No") or ("n"):
>         print("Oh...well im sure you can find something else to do")
>     else:
>         print("You just stand  there")
> town()
>
>
>
> i don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end? i think anway.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#52232

FromKris Mesenbrink <krismesenbrink@gmail.com>
Date2013-08-08 15:20 -0700
Message-ID<5674a7c0-d046-43a5-a998-29ada6c08412@googlegroups.com>
In reply to#52185
WOW as if it was something as easy as that,i had been looking for  awhile on what i was doing wrong. as it seems i just don't know my way around if statements at all, thank a bunch for this. makes everything else i have been code work

thanks again

[toc] | [prev] | [next] | [standalone]


#52235

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-08-08 19:37 -0400
Message-ID<mailman.379.1376005040.1251.python-list@python.org>
In reply to#52232
On Thu, 8 Aug 2013 15:20:01 -0700 (PDT), Kris Mesenbrink
<krismesenbrink@gmail.com> declaimed the following:

>WOW as if it was something as easy as that,i had been looking for  awhile on what i was doing wrong. as it seems i just don't know my way around if statements at all, thank a bunch for this. makes everything else i have been code work

	Actually, it is /not/ the if you misunderstood -- but the precedence of
Boolean algebra (the conditional can be lifted out of the if and put on a
separate line):

cond = something == x or y or z

if cond:

	The flaw would be in the first statement, not the IF.

	Python does support shortcut form of:

cond = value <= x <= other 

(note: you can use other comparisons, but know what you are doing)

	That expands to the equivalent of

cond = value <= x and x <= other
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#52234

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-08-08 19:31 -0400
Message-ID<mailman.378.1376004726.1251.python-list@python.org>
In reply to#52171
On Thu, 8 Aug 2013 10:34:57 +0000 (UTC), Dave Angel <davea@davea.name>
declaimed the following:

>
>Or less typing:
>
>if answer in ("yes", "Yes", "y"):
>

	Or (IF there is no chance for ambiguity)

	if answer.lower().startswith("y"):

	If there is ambiguity, provide enough of the matchword to determine the
unique option...

	if answer.lower().startswith("ye"):
		do_Yell_Action(rest_of_input_line)
	elif answer.lower().startswith("yi"):
		do_Yield_Action(rest_of_input_line)

	Granted, if parsing command options, you might want to lift the
.lower() out of the "if" conditions

	answer = answer.lower()
	if answer.startswith(...

{to expand (2.x syntax)}

	command = raw_input("Enter command> ")
	command = command.lower().split()

	(answer, args) = (command[0], command[1:])

	operation = None
	if answer.startswith("ye"):
		operation = do_Yell_Action
	elif answer.startswith("yi"):
		operation = do_Yield_Action
	elif answer.startswith("dr"):
		operation = do_Drop_Action
...

	if operation is not None:
		operation(args)
	else:
		print "That is not a valid command"


>
>
>
>>         print("You set out for the Coffeington Caves")
>>     elif answer == ("no") or ("No") or ("n"):
>>         print("Oh...well im sure you can find something else to do")
>>     else:
>>         print("You just stand  there")
>> town()
>>
>>
>>
>> i don't know why the "elif" or "else" part of the "if statment" wont trigger. what ends up happening is that regardless of what answer you put in input it will always print out "you set out for the Coffeington Caves". whats supposed to happen is if you say "no" it should just end? i think anway.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web