### Thread: So So confused with this loop...

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
3
Rep 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. 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.
3. 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.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2012
Posts
39
Rep Power
3
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.