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 January 15th, 2013, 06:21 AM
weonthewave weonthewave is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 3 weonthewave User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 26 sec
Reputation Power: 0
Question So So confused with this loop...

I am so confused here.
This loop is from Learn Python the Hard Way...

_________________________

def gold_room():
...indent....print "This room is full of gold. How much do you take?"
...indent....next = raw_input("> ")
...indent....if "0" in next or "1" in next:
...indent.......how_much = int(next)
...indent....else:
...indent........dead("Man, learn to type a number.")

...indent....if how_much < 50:
...indent.......print "Nice, you're not greedy, you win!"
...indent.......exit(0)
...indent....else:
...indent.......dead("You greedy bastard!")

_________________________


To simplify I write it like this.

_________________________


def gold_room():
...indent....print "This room is full of gold. How much do you take?"
...indent....next = raw_input(">")
...indent....if "0" in next or "1" in next:
...indent.......how_much = int(next)
...indent....else:
...indent.......print "aaaaaaaaaaaaa"

...indent....if how_much < 50:
...indent.......print "bbbbbbbbbbbbb"
...indent....else:
...indent.......print "cccccccccccccccccc"

gold_room()

_________________________


It makes sense how it works when I put 0, 1.

1. What doesn't make sense 1
"how_much" is getting unboundlocalerror when I put any number from 2 to 49.
It does make sense that it gets error, because "how_much" can not be assigned or defined without putting 0 or 1.

But I don't understand why he writes code like this and expecting how to make it work?


2. What doesn't make sense 2
How come the code prints out "cccccccccccc" when I put any number from 50, 51 ...
I have no idea how "how_much" comes to have a value even if I do not put 0 nor 1 so I thought it can't have access to the line "how_much = int(next)"

Without going through the line "how_much = int(next)"...
how the heck "how_much" gets a value assigned from "next"--see when I input 51, it goes through raw_input and be assigned to "next", not to "how_much"


Please help me. I am in a big chaos.
feel frustrated

Reply With Quote
  #2  
Old January 15th, 2013, 08:36 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 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 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Thank you for trying to indent. Please read link at my signature for [ code ] tags instructions about preserving white space.

Note that the variable next is a string. Only in the cases that that string contains a 0 or 1 does python assign a value to the how_much variable.

Otherwise the code reaches
if how_much < 50:
without a value for how_much, causing the unknown variable error you found.

Fix the problem by assigning a value to how_much before python tries to read its value.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old January 29th, 2013, 09:55 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 499 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 4 Days 3 h 19 m 2 sec
Reputation Power: 63
Put code tag [/code] at the end of your code and code tag [code] at the start of your code. Had to reverse the order or they would be active and not show.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Reply With Quote
  #4  
Old January 30th, 2013, 08:13 AM
Quackajack Quackajack is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 39 Quackajack User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 49 m 43 sec
Reputation Power: 1
As b49P23TIvg said, you are only checking if the entered amount has either a 0 or a 1 in it rather than checking the user has entered a number. "0" in next or "1" in next would return true if they entered "spam1" for instance.

You could use isdigit() to check what was entered only contains digits as follows. This is probably what you intended but is not necessarily the best approach.

Code:
if next.isdigit():
    how_much = int(next)
else:
    # no number has been entered


A more pythonic way is just to go ahead with the assignment to an integer and catch the exception. You could achieve your aim with just the following code.

Code:
try:
    how_much = int(next)
    if how_much < 50:
        print("Keep the gold")
    else:
        print("Greedy")
except ValueError:
    print("Enter a number")


I would also recommend that you use a better variable name than next for the string entered.
Comments on this post
b49P23TIvg agrees: next being a builtin function. Good point.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > So So confused with this loop...

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