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.

 Dev Shed Forums Sponsor:
#1
January 15th, 2013, 06:21 AM
 weonthewave
Registered User

Join Date: Jan 2013
Posts: 3
Time spent in forums: 41 m 26 sec
Reputation Power: 0
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:

_________________________

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"

feel frustrated

#2
January 15th, 2013, 08:36 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,458
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!

#3
January 29th, 2013, 09:55 PM
 Dietrich
Contributing User

Join Date: Feb 2005
Posts: 499
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

#4
January 30th, 2013, 08:13 AM
 Quackajack
Contributing User

Join Date: Jul 2012
Posts: 39
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.

 Viewing: Dev Shed Forums > Programming Languages > Python 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 Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

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