[Tutor] python game error

2018-10-13 Thread Mariam Haji
Hi guys,

I am still on the learn python the hard way.
I built the below on python 2.7 and my OS is windows 7.

I am getting this error:
Traceback (most recent call last):
  File "ex43.py", line 205, in 
a_game.play()
  File "ex43.py", line 21, in play
next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'


Main code is as, an I have highlighted line 21 and 205 in green below.


from sys import exit
from random import randint

class Scene(object):

def enter(self):
print "This scene is not yet configured. Subclass it and implement enter()."
exit(1)
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene()
while True:
print "\n"
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
class Death(Scene):

quips = [
"You died. You Kinda suck at this.",
"Your mum would be proud if she were smarter.",
"Such a looser.",
"I have a small puppy that's better at this."
]

def enter(self):
print  Death.quips[randint(0, len(self.quips)-1)]
exit(1)
class CentralCorridor(Scene):
def enter(self):
print "The Gothons of Planet Percal #25 have invaded your ship and
destroyed"
print "Your entire crew, you are the only surviving memeber and your last"
print "Mission is to get the neutron destruct a bomb from the weapons
Armory"
print "Put it in the bridge and blow the ship up after getting into an "
print "escape pod"
print "\n"
print "You're running down the central corridor to the Weapons Armory when"
print "a Gothon jumps out, red scaly skin, dark grimy teeth, and evil clow
costume"
print "flowing around his hate filled body. He's blocking the door to the"
print "Armoury and about to pull a weapon to blast you."
print "Do you shoot!, dodge!, or tell a joke? enter below."
action = raw_input("> ")
if action == "shoot!":
print "Quick on the draw you yank out your blaster and fire it at the
Gothon."
print "His clown costume is flowing and moving around his body, which
throws"
print "off your aim. Your laser hits his costume but misses him entirely.
This"
print "completely ruins his brand new costume his mother bought him, which"
print "makes him fly into a rage and blast ou repeatedly in the face until"
print "you are dead. Then he eats you"
print 'death'
elif action == "dodge!":
print "Like a world class boxer you dodge, weave, slip and slide right"
print "as the Gothon's blaster cranks a laser past your head"
print "In the middle of your artful dodge your foot slips and you"
print "bang your head on the metal wall and you pass out"
print "You wake up shortly after, only to die as the Gothon stomps on"
print "your head and eats you"
print 'death'
elif action == "tell a joke":
print "Lucky for you they made you learn Gothon insults in the academy"
print "you tell the one Gothon joke you know"
print "Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf
nebhaq gur ubhfr."
print "The Gothon stops, tries not to laugh, he busts out laughing and
can't move."
print "While he is laughing you run up and shoot him square in the head"
print "putting him down, then jump through the Weapon Armory door."
return 'laser_weapon_armoury'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
class LaserWeaponArmory(Scene):

def enter(self):
print "You do a drive roll into the weapon Armory, crouch and scan the room"
print "for more Gothons that might be hiding. It's dead quiet, too quiet"
print "You stand up and run to the far side of the room and find the"
print "neutron bomb in it's container. There's a keypad lock on the box"
print "Wrong 10 times then the lock closes forever and you can't"
print "get the bomb. The code is 3 digits"
code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
guess = raw_input("[keypad]> ")
guesses = 0
while guess != code and guesses < 10:
print "BZZZEE!"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "The container clicks open and the seal breaks, letting gas out."
print "You grab the neutron bomb and run as fast as you can to the"
print "bridge where you must place it in the right spot"
return 'the_bridge'
else:
print "The lock buzzes one last time then you hear a sickening"
print "melting sound as the mechanism is fused together"
print "You decide to sit there and finally the Gothons blow up the"
print "ship form their ship and you die."
return 'death'
class TheBridge(Scene):

def enter(self):
print "you bust into the bridge with the neutron destruct bomb"
print "under your arm and surprises 5 Gothons who are trying to"
print "take control of the ship. Each of them has an even uglier"
print "clown costume then the last. They haven't pulled their"
print "weapons out yet, as they see the active bomb under your"
print "and don't want to set it off."
action = raw_input("> ")
if action == "throw the bomb":
print "In panic you throw the bomb at the group of Gothons"
print "and make the 

[Tutor] Statistics with python

2018-10-13 Thread Mariam Haji
Hi guys,

I have a statistics question where I am to calculate the probability of a
sample dataset.

I have done the coding bit but I am not sure if I did the right thing, I
just need confirmation before I submit it.

the question is as:
If a sample of 50 patients is taken from a dataset what is the probability
that we will get a patient above the age of 56?

So I know my sample mean is 50 and my no is 56
to get std I manually did 50/√56 (that's 50/square root of 56) I got the
answer as 6.66

So my mean is 50 and std is 6.66

Then I did the below to get the z score and probability using scipy.stats
as st

m=50
s=6.66

z1 = (56-m)/s

p1 = st.norm.sf(z1)
print ('Probability of patient above the age of 56 is:', (p1))

Probability of patient above the age of 56 is: 0.183820506093897


Am i missing something or did I do it right?


Thanks in Advance.

-- 
*Regards,*
*Mariam.*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python game error

2018-10-13 Thread Steven D'Aprano
On Sat, Oct 13, 2018 at 11:25:20AM +0300, Mariam Haji wrote:
> Hi guys,
> 
> I am still on the learn python the hard way.
> I built the below on python 2.7 and my OS is windows 7.
> 
> I am getting this error:
> Traceback (most recent call last):
>   File "ex43.py", line 205, in 
> a_game.play()
>   File "ex43.py", line 21, in play
> next_scene_name = current_scene.enter()
> AttributeError: 'NoneType' object has no attribute 'enter'

The current_scene variable currently is set to None, instead of athe 
value that you think it has.

Try inserting the line 

print repr(current_scene), type(current_scene)

just before line 21. Then you need to work out why it is set to None, 
instead of whatever you expect it to be.

By the way, you shouldn't expect us to hunt through dozens or hundreds 
of lines of code trying to debug your program for you. We're volunteers, 
not being paid to do this. You should give us a *minimum* (small!) 
example of your program.

That might mean keeping two copies, one which has all the irrelevant 
code removed. (Like the dozens of calls to print -- not one 
single one of those lines of code could possibly have anything 
to do with the bug in your program.)

Removing that irrelevant code, cutting it down to a minimum, may even 
help you find the bug yourself! Please read this for more information:

http://sscce.org/

Even though this is written for Java programmers, the principles also 
apply to Python.


> Main code is as, an I have highlighted line 21 and 205 in green below.

Have you? That explains why the indentation is messed up for me, making 
the code impossible to understand correctly. And the green doesn't show up 
in my email program.

Code is PLAIN TEXT, with no formatting. When you add formatting, like 
colours, bold or italics, fancy fonts, animmated paperclips, etc, your 
email program will destroy the indentation and make it unusable.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Statistics with python

2018-10-13 Thread Steven D'Aprano
On Sat, Oct 13, 2018 at 11:32:09AM +0300, Mariam Haji wrote:

> the question is as:
> If a sample of 50 patients is taken from a dataset what is the probability
> that we will get a patient above the age of 56?

That depends on the data set.

How is it distributed? There are dozens of likely distributions: 
Poisson, Cauchy, binomial, multinomial, hypergeometric, Zipf, Weibull, 
Pareto, Student's t, uniform, and many, many, many, MANY more:

https://en.wikipedia.org/wiki/List_of_probability_distributions 


> So I know my sample mean is 50 and my no is 56
> to get std I manually did 50/√56 (that's 50/square root of 56) I got the
> answer as 6.66

If you use too many abbreviations, especially the wrong abbreviations, 
it is hard to understand you.

Do you mean that n, the number of samples ("no") is 56?

What's std? Standard deviation, standard error, Sexually Transmitted 
Disease? Something else?

It looks like you are dividing the sample mean by the square root of the 
sample size, but I don't know why.

 
> So my mean is 50 and std is 6.66
> 
> Then I did the below to get the z score and probability using scipy.stats
> as st
> 
> m=50
> s=6.66
> 
> z1 = (56-m)/s

You're subtracting the average data point from the number of samples, 
which is meaningless. And the value of s seems to be meaningless too.

It is possible that I have misunderstood you. Please try explaining your 
steps more carefully, without using abbreviations we might not get. I've 
tried to guess what you mean as best I can, but I might not have 
succeeded.


Thank you.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python game error

2018-10-13 Thread Peter Otten
Mariam Haji wrote:

> Hi guys,
> 
> I am still on the learn python the hard way.
> I built the below on python 2.7 and my OS is windows 7.
> 
> I am getting this error:
> Traceback (most recent call last):
>   File "ex43.py", line 205, in 
> a_game.play()
>   File "ex43.py", line 21, in play
> next_scene_name = current_scene.enter()
> AttributeError: 'NoneType' object has no attribute 'enter'

The value of current_scene is None here. If you dig a bit in your code 
looking for places where that can happen you end up with

> class Map(object):
> scenes = {
> 'central_corridor': CentralCorridor(),
> 'laser_weapon_armory': LaserWeaponArmory(),
> 'the_bridge': TheBridge(),
> 'escape_pod': EscapePod(),
> 'death': Death()
> }
> 
> def __init__(self, start_scene):
> self.start_scene = start_scene
> 
> def next_scene(self, scene_name):
> return Map.scenes.get(scene_name)

Map.scenes is a dict, and its get() method returns None when the lookup for 
a key fails. If you pass an unknown scene name to the next_scene() method it 
returns None rather than a Scene instance.

As a first step to fix this I recommend that you use the item lookup 
operator instead of the method:

  def next_scene(self, scene_name):
  return Map.scenes[scene_name]

This will fail immediately, and the error message should be more 
informative. I got:

Traceback (most recent call last):
  File "./game_mariam.py", line 181, in 
a_game.play()
  File "./game_mariam.py", line 20, in play
current_scene = self.scene_map.next_scene(next_scene_name)
  File "./game_mariam.py", line 173, in next_scene
return Map.scenes[scene_name]
KeyError: 'laser_weapon_armoury'

That is, you need to decide if you want British or American orthography.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python game error

2018-10-13 Thread Bob Gailer
suggestions:
1-Use triple-quoted strings:
  print """take the
short-cut!""'

2 - make the program much simpler to start with. The usual approach to
developing programs like this is to start simple get the simple things
working right then add more complicated scene descriptions.

Even better: separate data from logic. Create a sqlite database in which
you store the information about the various scenes. Then write a program
that does nothing but access the various database elements. This will
result in much much easier to read and maintain code, and much much easier
to read and maintain the description of the scenes. I realize this may
sound like a big chunk but it is well worth the effort to learn how to do
it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Statistics with python

2018-10-13 Thread Oscar Benjamin
On Sat, 13 Oct 2018 at 11:23, Mariam Haji  wrote:
>
> Hi guys,

Hi Mariam

> the question is as:
> If a sample of 50 patients is taken from a dataset what is the probability
> that we will get a patient above the age of 56?

I can think of several ways of interpreting this:

(a): You have a dataset consisting of 50 patients. You want to know
the probability that a patient chosen from that sample will be above
the age of 56.

(b): You have a dataset consisting of 50 patients. You consider it to
be representative of a larger population of people. You would like to
use your dataset to estimate the probability that a patient chosen
from the larger population will be above the age of 56.

(c): You have a larger dataset consisting of more than 50 patients.
You want to know that probability that a sample of 50 patients chosen
from the larger dataset will contain at least (or exactly?) one person
above the age of 56.

(d): You have a larger dataset, but you will only analyse a sample of
50 patients from it. You want to use statistics on that sample to
estimate the probability that a patient chosen from the larger dataset
will be above the age of 56.

I can list more interpretations but I think it would be better to wait
for you to clarify.

> So I know my sample mean is 50

Do you mean that you separately know that the sample mean is 50? Or do
you mean that you know it's 50 because of what you stated above?

Above you stated that you have a sample *size* of 50 and that doesn't
imply that the sample *mean* is 50.

> and my no is 56
> to get std I manually did 50/√56 (that's 50/square root of 56) I got the
> answer as 6.66

It's possible that you are not using the correct terminology here but
otherwise this isn't correct. If you had a sample *standard deviation*
of 50 and a sample *size* of 50 then 50/sqrt(56) would give you the
standard error. I am not sure that you did actually want to do that
though.

> So my mean is 50 and std is 6.66

I'm not sure that this is correct...

> Then I did the below to get the z score and probability using scipy.stats
> as st
>
> m=50
> s=6.66
>
> z1 = (56-m)/s
>
> p1 = st.norm.sf(z1)
> print ('Probability of patient above the age of 56 is:', (p1))
>
> Probability of patient above the age of 56 is: 0.183820506093897

The part above looks correct if we assume we are choosing a single
patient whose age is normally distributed with mean 50 and standard
deviation 6.66. I'm not sure these assumptions are correct though.

--
Oscar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor