Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 23rd, 2012, 05:11 PM
Nick Wyden Nick Wyden is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 44 Nick Wyden Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
Just a quick Question

So I am trying to do a conditional statement using boolean logic my code is

Rock = True

Goth = False

Pop = True


music = input ("what is your fav music ")

if music == True:

print ("That is Good Music")

else:
print (" No that sucks")



However when I run this script every time I input Rock , or I input Pop the programs prints the function assigned to else

what am I doing wrong?

Reply With Quote
  #2  
Old September 23rd, 2012, 05:54 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
The answer depends upon whether you're using python 3 or an earlier version.

In python 3 input returns a string which is never equal to the Boolean data type value True and so the else event always happens.

In python 2 input evaluates the string you type, and returns that. Thus, in python2 the entries of
True
or
1
would cause the true branch of the if statement to execute.

(or if you happen to have a variable named a with the value of 1 assigned to it you could enter a as well.)

[edit] false sentence removed [/edit]
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : September 24th, 2012 at 04:10 PM.

Reply With Quote
  #3  
Old September 23rd, 2012, 08:27 PM
Nick Wyden Nick Wyden is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 44 Nick Wyden Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
The answer depends upon whether you're using python 3 or an earlier version.

In python 3 input returns a string which is never equal to the Boolean data type value True and so the else event always happens.

In python 2 input evaluates the string you type, and returns that. Thus, in python2 the entries of
True
or
1
would cause the true branch of the if statement to execute.

(or if you happen to have a variable named a with the value of 1 assigned to it you could enter a as well.)

Study the tutorials a while longer, you're quite near the clueless level at which we all begin.


alright thanks two quick things 1) I got a book called python 3 for absolute beginners is this good for learning and 2) so I would have to convert the string into a numerical data string right
Comments on this post
b49P23TIvg agrees: I apologize for earlier negative remarks.

Reply With Quote
  #4  
Old September 23rd, 2012, 08:38 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
Yes, exactly.

Oh, you are using python3. Reading this with a python3 point of view I've inserted the error trapped eval statement
Code:
Rock = True
Goth = False 
Pop = True
string = input ("what is your fav music ")

try:
    music = eval(string)
except:
    music = False

if music == True:
    print ("That is Good Music") 
else: 
    print (" No that sucks") 

Reply With Quote
  #5  
Old September 24th, 2012, 03:45 PM
Nick Wyden Nick Wyden is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 44 Nick Wyden Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
Smile

Thanks

Reply With Quote
  #6  
Old September 28th, 2012, 07:43 PM
Nick Wyden Nick Wyden is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 44 Nick Wyden Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 4 h 10 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by Nick Wyden
Thanks



OHHHHH ok so its illogical to try to do a else statement in that

context. This forum is honestly just so helpful

Reply With Quote
  #7  
Old September 29th, 2012, 01:40 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
How have you made four threads and still not know how to use code tags? Put your code inside [code][/code] or use the '#' button in the editor.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Just a quick Question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap