5 Replies - 639 Views - Last Post: 04 August 2012 - 02:02 PM Rate Topic: -----

#1 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

If Statement stuff

Posted 03 August 2012 - 07:53 PM

a = raw_input("Enter either: 'Y' or 'N': ")
if a == "Y":
    print "Hello"
elif a == "N":
    print "Goodbye"


Is there any way that if a = y, it continues to the code that is not in the if, elif part of the code?
Or assign the output to a value, but only if they do a certain one? For instance, if it outputs "Hello", you assign to value b?

Is This A Good Question/Topic? 0
  • +

Replies To: If Statement stuff

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,515
  • Joined: 28-November 09

Re: If Statement stuff

Posted 03 August 2012 - 08:26 PM

Simply assign a value to a variable in the condition that you want it rendered.
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: If Statement stuff

Posted 03 August 2012 - 08:50 PM

I don't really understand your question. If you want your code to be case insensitive, that's an easy change:
a = raw_input("Enter either: 'Y' or 'N': ")
if a.upper() == "Y":
    print "Hello"
elif a.upper() == "N":
    print "Goodbye"




if you're saying you want to skip the condition if a == 'y' :
a = raw_input("Enter either: 'Y' or 'N': ")

if a != "y":
    if a == "Y":
        print "Hello"
    elif a == "N":
        print "Goodbye"
else:
    #code that skips the if statement


This post has been edited by atraub: 03 August 2012 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: If Statement stuff

Posted 04 August 2012 - 01:23 AM

No, just that if a == y, then the output would be hello. Is there any way to automatically assign the "hello" output to another variable?
Also, if you enter n, then the out put would be "goodbye". Can you make it so that the code only continues if the output is "hello" without putting the entire code in the if statement?
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: If Statement stuff

Posted 04 August 2012 - 09:08 AM

Sure, none of this is very hard to do. Assigning a string to a variable is as simple as output = "hello"

As for moving things out of the condition, that's doable too. What you might want to think about is something called an early exit case. This is when you end your program or function at an earlier point. You could return from your function a == 'N'. Once you've done that, nothing in your function is accessible if the user chose 'N', thus it wouldn't need to be indented. Give this a shot on your own and show us what you come up with and we'd be happy to help you out more if you need it.

SIDENOTE:
Early exit cases are often considered poor form. I personally think they're fine so long as they aren't abused.

This post has been edited by atraub: 04 August 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#6 Region  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-July 12

Re: If Statement stuff

Posted 04 August 2012 - 02:02 PM

Ok! Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1