10 Replies - 1802 Views - Last Post: 11 August 2015 - 12:24 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Stuck on lesson #35 student drills learn python the hard way.

Posted 10 August 2015 - 02:12 PM

I've already finished and made the program work on lesson #35. I'm on the student drills where the lesson asks the student to add more to the game try to both simplify and expand it. I thought I'd challenge myself and try something I don't think anyone has thought of doing with this lesson. I figured I'd put a random feature in the program to make the result change depending on what it presented. So, if a person decided to scare the bear, take its honey or taunt it the player would either get eaten, slapped or the bear would move allowing the player to continue to the gold_room. Here is my problem, the program works, but, not exactly how I would want it to work. The problem I have is with the function called events(). I put some notes within to explain the problem and where the problem is.
from sys import exit
import random

def start():
	print"""you walk into a dark room.
There is a door to your right and left.
which one will you take?
             """
	choice=raw_input("> ")
	if choice=='left':
		bear_room()
	elif choice=='right':
		cthulhu_room()
	else:
		dead("You stumble around the room until you starve")

def bear_room():
        print"""There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?.\n
Will you..."""
        print'Try to scare the bear?'
        print'Take its honey?'
        print'Taunt the bear?'
        print'Leave the room?'
        while True:
                choice=raw_input("> ")
                if "leave" in choice:
                        start()
                else:
                        events()
						
def events():
        a=("the bear slaps your face off","the bear moves","The bear gets pissed off and chews your face off")
        """I can't figure out how to shorten 'a'. I've tried everything I can think of
and the only way it will work is this way"""
        x=random.choice(a)
        print x
        if x in ("slaps","pissed"):
                dead("Nice going, you're dead")
        elif x in ("moves"):
                gold_room()
        """When random() picks one of the choices it prints it (Which is whatI wanted) but,
it wont go to the function called dead(why): or the function gold_room().
					 
def gold_room():
	print "This room is full of gold. How much do you take? "
	choice =raw_input("> ")
	if "0" in choice or "1" in choice:
		how_much = int(choice)
	else:
		dead("Man, learn to type a number.")
	if how_much < 50:
		print "Nice, you're not greedy, you win!"
		exit(0)
	else:
		dead("You greedy bastard!")
		
def cthulhu_room():
	print "Here you see the great evil Cthulhu."
	print "He, it, whatever stares at you and you go insane."
	print "Do you flee for your life or eat your head?"
	choice=raw_input("> ")
	if "flee" in choice:
		start()
	elif "head" in choice:
		dead("Well that was tasty!")
	else:
		cthulhu_room()
		
def dead(why):
	print why, "good job!"
	exit(0)
	

start()



Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on lesson #35 student drills learn python the hard way.

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 10 August 2015 - 03:14 PM

I'm not sure what you mean by shortening 'a', I recommend you lengthen it: use meaningful variable names.
if x in ("slaps","pissed"):

This doesn't work. x is a sentence, it won't be in the single word "slaps".

You could reverse it:
if ("slaps" in x) or ("pissed" in x):
	print "it is"
else:
	print "it is not"

although I would probably switch it around again:
if "moves" in x:

This post has been edited by andrewsw: 10 August 2015 - 03:15 PM

Was This Post Helpful? 2
  • +
  • -

#3 DblAAssassin   User is offline

  • D.I.C Regular

Reputation: 43
  • View blog
  • Posts: 322
  • Joined: 11-May 13

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 10 August 2015 - 04:32 PM

I agree with andrewsw, could you clarify what you mean by "shorten a"?
Was This Post Helpful? 0
  • +
  • -

#4 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 10 August 2015 - 06:55 PM

I was meaning that I didn't like having such a long string in there. I can't remember where but I read some place that its a sign of a bad programmer to have such long strings in their programs. I thought that the string was a little too long.

Would you also be able to help me with my other question?. I put a random.choice function in my program so that every time a person played that game it would yield a different result. Every time I select any of the options in that function instead of moving to the next function dead(why) it keeps looping the same results over and over again.
from sys import exit
import random

def gold_room():
	print "This room is full of gold. How much do you take? "
	choice =raw_input("> ")
	if "0" in choice or "1" in choice:
		how_much = int(choice)
	else:
		dead("Man, learn to type a number.")
	if how_much < 50:
		print "Nice, you're not greedy, you win!"
		exit(0)
	else:
		dead("You greedy bastard!")
		
def bear_room():
        print"""There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?.\n
Will you..."""
        print'Try to scare the bear?'
        print'Take its honey?'
        print'Taunt the bear?'
        print'Leave the room?'
        while True:
                choice=raw_input("> ")
                if "leave" in choice:
                        start()
                else:
                        events()
						
def events():
        a=("the bear slaps your face off","the bear moves","The bear gets pissed off and chews your face off")
        x=random.choice(a)
        print x
        if x in ("slaps","pissed"):
                dead("Nice going, you're dead")
        elif x in ("moves"):
                gold_room()
	#This is another place I am stuck. The random.choice does what I want (randomly chooses 
        #the options). I can't move it to the option to go to the next function dead(why). 			
def cthulhu_room():
	print "Here you see the great evil Cthulhu."
	print "He, it, whatever stares at you and you go insane."
	print "Do you flee for your life or eat your head?"
	choice=raw_input("> ")
	if "flee" in choice:
		start()
	elif "head" in choice:
		dead("Well that was tasty!")
	else:
		cthulhu_room()
		
def dead(why):
	print why, "good job!"
	exit(0)
	
def start():
	print "You are in a dark room."
	print "There is a door to your right and left."
	print "Which one do you take?"
	choice=raw_input("> ")
	if choice=="left":
		bear_room()
	elif choice=="right":
		cthulhu_room()
	else:
		dead("You stumble around the room until you starve.")

		
start()

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 10 August 2015 - 07:03 PM

Quote

I can't remember where but I read some place that its a sign of a bad programmer to have such long strings in their programs.


I would certainly like to get rid of the strings from the source code. The correct number of string literals in your source code is zero. One reason: if you have literal strings in your source, then the only person who can change that language is a programmer - but the only person who should be changing it is almost certainly not a programmer, but a UI designer or a marketing person or someone like that. Another reason: Internationalization becomes quite impossible when you've salted the program text throughout the code in this way. Another reason: it tends to make abstraction impossible.

However, you don't improve a piece of code by making the variable names less communicative. The name of the variable should tell me exactly what it represents in the code. This way I can see your intent and your implementation at the same time, and if they disagree, I can easily see where you've gone wrong. If I have to determine your intent from your implementation, there's no way for me to know what's wrong with your implementation.
Was This Post Helpful? 3
  • +
  • -

#6 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 11 August 2015 - 01:40 AM

Quote

Every time I select any of the options in that function instead of moving to the next function dead(why) it keeps looping the same results over and over again.

What part of the code are you talking about? You need to explain what is happening more clearly.

But you haven't taken notice of the code and explanation in my previous post.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 11 August 2015 - 01:51 AM

Quote

it keeps looping the same results over and over again.


There is only one loop structure in your code, while True
The only way this loop will end is with the two exit() statements
exit() occurs in the dead() function, do you ever see the message why, "good job!"?
exit() also occurs in gold_room(), do you ever see the message "Nice, you're not greedy, you win!"?

You need to be able to trace through your code. You have to follow and check every possible path.

In addition to the corrections I mentioned earlier, I would also look into the assignment (or non assignment) of how_much.



I appreciate that you probably won't want to do this now but, going forward, the program could be restructured so that there is only one, natural, exit point, and explicit calls to exit() are removed.

This post has been edited by andrewsw: 11 August 2015 - 01:55 AM

Was This Post Helpful? 2
  • +
  • -

#8 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 11 August 2015 - 02:12 AM

cthulhu?

Attached Image

(I think I've dated this creature)
Was This Post Helpful? 0
  • +
  • -

#9 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 11 August 2015 - 11:30 AM

I think all men have dated one in one time or another in their lives. You can tell when you date one because they suck your soul and leave you as an empty shell. :)
Was This Post Helpful? 0
  • +
  • -

#10 albert003   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 993
  • Joined: 15-December 14

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 11 August 2015 - 11:54 AM

View Postandrewsw, on 11 August 2015 - 02:12 AM, said:

cthulhu?

Attachment ct.jpg

(I think I've dated this creature)



Thank you, thank you thank you, thank you!!!! I got it to work after reading your suggestions!!!. Thank you!!!!!
Was This Post Helpful? 1
  • +
  • -

#11 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Stuck on lesson #35 student drills learn python the hard way.

Posted 11 August 2015 - 12:24 PM

good stuff
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1