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 August 30th, 2012, 08:43 PM
amjones amjones is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 amjones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 18 sec
Reputation Power: 0
Extremely basic code help

I am just starting to program, about a day in and I wrote up this little code using the stuff I have learned so far. The code has no errors but it does not work properly and I can not seem to understand why. I want the code to give the logical results for the answers put into it. I am using 2.7

Code:
print 'Is that an animal'
thing = raw_input('')
print 'What kind?'
animal = raw_input('')

if thing == 'yes':
    if animal == 'monkey':
        print 'Oh wow!'
    else:
        print 'Never heard of it'

else:
    print 'Get it outta here then!'

Reply With Quote
  #2  
Old August 30th, 2012, 09:51 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
Please post the dialogue you're hoping to see.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old September 1st, 2012, 08:44 PM
amjones amjones is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 2 amjones User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 18 sec
Reputation Power: 0
I'm just trying to get the text to print if certain conditions are met. So, if the program asks "Is that an animal?", and the raw_input is "yes", then the program will reply "What kind?". If the raw_input is anything but "yes", then the program will reply
"get it outta' here then!". If the raw_input to "What kind?" is "monkey', then the program will reply "Oh, wow!". If the raw_input is anything but "monkey" then the program will reply "Never heard of it".

Reply With Quote
  #4  
Old September 1st, 2012, 09:57 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
Thank you for the clear description.


Here's one way
Code:
def monkeys():
    if 'yes' != raw_input('Is that an animal?').lower():
        return "get it outta' here then!"
    if 'monkey' == raw_input('What kind?').lower():
        return 'Oh, wow!'
    return 'Never heard of it'

if '__main__' == __name__:
    print monkeys()
else:
    print 'use monkeys() function in this module'


Here's another way to write the logic in a style similar to your post. The essential change is that I've moved the "What kind?" question into one side of the test. The question arises depending on the first question.
Code:
if not ('yes' == raw_input('Is that an animal?').lower()):
    print "get it outta' here then!"
else:
    if 'monkey' == raw_input('What kind?').lower():
        print 'Oh, wow!'
    else:
        print 'Never heard of it'



*Oh, I used a negative test for stylistic reasons.
Code:
# I rewrite
if condition:        # poor style.  "if" is physically far from "else"
    many
    lines
    of
    code
else:
    fewer lines of code


# as
if not (condition):    # put "else" close to "if"
    fewer lines of code
else:
    many
    lines
    of
    code



**Oh, some people swear that a function should have one return. (Dykstra, in particular as I recall.) I happen to prefer short functions with few indentation levels. Multiple return statements don't scare me. Now the comefrom statement---that's another beast.

Last edited by b49P23TIvg : September 1st, 2012 at 10:13 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Extremely basic code help

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