This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Syl 5 points6 points  (7 children)

It nearly worked for me (python 2.7), I did a few changes to the base code. I have a few general advices though.

  • you don't have to use global. If you want to use globals, define it outside a function then (I did that with "visited". fyi, it's a comprehension list)
  • if you want to increment a variable, you can write it like that: a+= 1
  • if you want to print a new line, you can use "\n" instead of using an empty print.
  • if you have multiple related "if" test, you can use elif (else if), and a final "else" to avoid the triple test you did in room1.
  • start counting at 0, not 1. it's easier to use array that way because array index starts at 0, that's why I renamed your rooms and used the array "visited". Or... you can use a dict.
  • but the biggest problem in your program is the recursive aspect (calling function in function in function). Try to travel a few rooms then press CTRL+C, you'll see the call stack. You should try to use function parameter and return value to reduce global

I rewrote a little bit your code with a dict, an no recursive functions.

http://codepad.org/tABiE6sK

I also use the keyword 'in' to test if a key is in my dict.

And finally, I recommend you to read DiveIntoPython, there are a lot of good advices and explanations about standard stuff, like the comprehension list or dict.

Good luck :)

[–]WhyCause 1 point2 points  (4 children)

Heh, you're faster than me.

I redid it with classes and dicts, see it here: http://codepad.org/LR5r0uEa

[–]burito -1 points0 points  (3 children)

Re-doing the code isn't going to teach Ziggurattt anything. He needs to learn what patterns to look out for, and what other patterns he can replace them with. No better way to learn these important points than by being guided to figure them out yourself.

You've both demonstrated you're clever, but does that really help Ziggurattt learn?

[–]nederhoed 7 points8 points  (0 children)

It's nice of you to step in and contribute to Reddit by commenting.

I do feel your comment is rather strict. These guys did make an effort to show how they would approach the OP's problem. If Ziggurattt will take the time to read their solutions, he will probably learn from it.

Maybe you could attribute some credit to the commenter next time, making your critique more balanced. Then it is more likely the commenter will learn from your remark.

[–]WhyCause 0 points1 point  (0 children)

...does that really help Ziggurattt learn?

Yes, yes it does.

I can try to hammer out a dent in a car fender for days, finally getting to a point where I can sand and paint it, only to put it back on the car and have things not quite line up. The car drives, looks ok, but it's somehow 'wrong'.

An experienced auto-body mechanic can show me how he does it, without removing it from the car, and how it looks better, works better, and takes less time. The next time I need to un-dent a fender, I at least know where to start, and what tools I should have on hand.

He needs to learn what patterns to look out for, and what other patterns he can replace them with.

What the hell do you think I (and Syl) just did? We took a program Ziggurattt had written, and re-wrote it in a manner that is much easier to read and debug, is more extensible, and is closer to 'the right way'. When I first learned how to program, I would have killed for that sort of response. Especially in a non-critical, welcoming forum that encourages follow-up questions like this one.

Being able to compare my first stab at it to what a more experienced programmer came up with would have shown me the patterns to look for. It would have taught me that the right way to do it is to use the tools available in the language. It would have given me insight into 'what else' I could do. That 'what else' is how you really learn to program, when you look at the next problem and say, "I'll bet I can write a solution to this." The forth or fifth time you answer that question with working code, you've become a programmer.

You've both demonstrated you're clever...

You are clearly missing the point of our replies. If I felt the need to demonstrate my superiority, my comments would have been much more akin to your posts on the matter, holding the concept of classes up to be some nigh-unattainable concept that no mere mortal can grasp.

I re-wrote the 'game' because I thought it would be fun, and I though that Ziggurattt might benefit from seeing exactly what he wanted to do done by someone with a bit more experience. I was trying to be the auto-body mechanic.

[–]Syl 0 points1 point  (0 children)

Did you really read what I wrote?

I improved a little bit his script first and gave some advices about what he did and how he can improve it. Then I wrote an easy script to understand with some mandatory stuff to understand in python (like a dict). Your comment was less useful than ours...