[Tutor] Beginner: Socket object and packets

2015-12-05 Thread Marc Eymard

Hi tutor,

I am trying to locate the first blank line in the first received packet 
when pinging an internet server using a socket object.


My assumption is there will be a mandatory blank line right after the 
http headers in accordance with the http protocol.


Consider the following:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect( ('www.py4inf.com/code/', 80) )
mysock.send('GET http://www.py4inf.com/code/' + ' HTTP/1.0\n\n')
data_str = mysock.recv(700)

My question:

Why is the following statement False when there is an actual blank line 
in the received packet:

'\n\n' in data

The statement 'any_string in data' works fine with any character except 
the double line drop i.e. '\n\n'.


Attached full script for your consideration.

Thanks in advance for your guidance,
Marc


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


[Tutor] Beginner - explaining 'Flip a coin' bug

2014-02-12 Thread Marc Eymard
Hello there,

I want to emulate a coin flip and count how many heads and tails when flipping 
it a hundred times.

I first coded coinflip_WRONG.py with "count_flips += 1" statement within the 
if/else block.
When running it, either returned values are wrong or the script seems to enter 
in an infinite loop showing no return values at all.

coinflip.py is a corrected version I worked out myself. I moved "count_flips+= 
1" out of if/else block and inserted it before if/else.

However, I still don't understand the bug since, in my understanding, both 
files are incrementing variable count_flips each time until the loop becomes 
false.

Can somebody explain the reason of the bug.
Cheers,

Marc
  #Coin Flip
#Flip a coin 100 times and count heads and tails

input('Press ENTER to flip a coin a hundred times')

import random

#set count values
count_heads = 0
count_tails = 0
count_flips = 0


while count_flips != 100:
coin_side = random.randint(1,2)
count_flips += 1

if coin_side == 1:
count_heads += 1
#count_flips += 1

else: count_tails += 1
#count_flips += 1

print('The coin returned',count_heads,'heads and',count_tails,'tails.')
#Coin Flip
#Flip a coin 100 times and count heads and tails

input('Press ENTER to flip a coin a hundred times')

import random

#set count values
count_heads = 0
count_tails = 0
count_flips = 0


while count_flips != 100:
coin_side = random.randint(1,2)
#count_flips += 1


if coin_side == 1:
count_heads += 1
count_flips += 1

else: count_tails += 1
count_flips += 1

print('The coin returned',count_heads,'heads and',count_tails,'tails.')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thanks - Beginner - explaining 'Flip a coin' bug

2014-02-15 Thread Marc Eymard
Hi David,

Thanks for your input about the logic of this little script of mine.

I confess I omitted the edge possibility and assumed heads or taisl only.

As I progress further with getting the foundation knowledge of the language 
itself, it is really appreciated to be corrected on what good programming is 
about regardless of the language.

Please keep commenting/helping whenever necessary.

Cheers,
Marc

Date: Fri, 14 Feb 2014 22:49:43 -0500
Subject: Re: [Tutor] Beginner - explaining 'Flip a coin' bug
From: dwightdhu...@gmail.com
To: marc_eym...@hotmail.com
CC: tutor@python.org

Here is a problem I've come across, from empirical evidence, that also relates 
to your equation. We always assume
 that their are always two probabilities, that a coin can be either head or 
tails. 


However, there are dynamics within a third realm of the dimensionality of the 
coin...it's not a two dimensional plane. So the planar probabilities in 
relation to the 'surface are' hold another possibility...the 'edge'.


The algorithm of applying the edge are up to you, but I've seen the coin land 
on it's curved edge more than once, so this function you've designed, should 
have more than two possibilities, within a complete algorithm to the real world 
functionality of the coin in question.



On Wed, Feb 12, 2014 at 10:25 AM, Marc Eymard  wrote:




Hello there,

I want to emulate a coin flip and count how many heads and tails when flipping 
it a hundred times.

I first coded coinflip_WRONG.py with "count_flips += 1" statement within the 
if/else block.

When running it, either returned values are wrong or the script seems to enter 
in an infinite loop showing no return values at all.

coinflip.py is a corrected version I worked out myself. I moved "count_flips+= 
1" out of if/else block and inserted it before if/else.


However, I still don't understand the bug since, in my understanding, both 
files are incrementing variable count_flips each time until the loop becomes 
false.

Can somebody explain the reason of the bug.

Cheers,

Marc
  

___

Tutor maillist  -  Tutor@python.org

To unsubscribe or change subscription options:

https://mail.python.org/mailman/listinfo/tutor




-- 
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com


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


[Tutor] Beginner - understanding randint arguments

2014-02-15 Thread Marc Eymard
Hello Tutor,

I need to generate a random integer between 0 and 100.

The range is supposed to be adjusted by my two variables:
low_range and high_range.

The logic of using the variables as part of the function arguments is to manage 
to get a smaller range each time the function is called excluding the possible 
repeat of the return value of randint.

Here is what happens in my script:

>>> import random
>>> low_range = -1
>>> high_range = 101
>>> random.randint(low_range + 1, high_range - 1)
56
>>> low_range
-1
>>> high_range
101

I was rather expecting:

 >>> low_range
0
>>> high_range
100

Can somebody explain why both low_range and high_range are still returning 
their initial values ?

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


[Tutor] Beginner - Clarifying 'understanding randint arguments'

2014-02-17 Thread Marc Eymard
Hi Tutor,

The previous elements I sent to the mailing list were incomplete and needs no 
answer from Tutor.

To clarify and re-phrase my script issue:

I want to code a game whereby the computer guesses either by luck or deduction 
a number I pick within [0, 100].

In attached machine_guess_number.py in which line 22 statement number = 
random.randint(low_range + 1, high_range - 1) doesn't narrow the range:

- to allow number deduction...
- ...  avoiding ValueError: empty range for randrange()

I also attached the output of the script leading to the error showing that the 
loop does not exit in time to avoid error by working out the answer based on a 
logical narrowing of the guessing range.

Hope this clarifies my previous email (see further down this note).

Thanks,
Marc


> Subject: Your message to Tutor awaits moderator approval
> From: tutor-ow...@python.org
> To: marc_eym...@hotmail.com
> Date: Sat, 15 Feb 2014 17:26:41 +0100
> 
> Your mail to 'Tutor' with the subject
> 
> Beginner - understanding randint arguments
> 
> Is being held until the list moderator can review it for approval.
> 
> The reason it is being held:
> 
> Post by non-member to a members-only list
> 
> Either the message will get posted to the list, or you will receive
> notification of the moderator's decision.  If you would like to cancel
> this posting, please visit the following URL:
> 
> 
> https://mail.python.org/mailman/confirm/tutor/8ee9ed6d473cbc6d77ddc7af36237a9cc3b1d4b3
> 

From: marc_eym...@hotmail.com
To: tutor@python.org
Subject: Beginner - understanding randint arguments
Date: Sat, 15 Feb 2014 16:25:34 +




Hello Tutor,

I need to generate a random integer between 0 and 100.

The range is supposed to be adjusted by my two variables:
low_range and high_range.

The
 logic of using the variables as part of the function arguments is to 
manage to get a smaller range each time the function is called excluding the 
possible repeat of the return value of randint.

Here is what happens in my script:

>>> import random
>>> low_range = -1
>>> high_range = 101
>>> random.randint(low_range + 1, high_range - 1)
56
>>> low_range
-1
>>> high_range
101

I was rather expecting:

 >>> low_range
0
>>> high_range
100

Can somebody explain why both low_range and high_range are still returning 
their initial values ?

Thanks,
Marc
  
  #Guess my Number game
#The computer has to guess the player's number by either luck or deduction
#number has to be within [0, 100]

import random

print('Pick a number between and 0 and 100...')
print('...and let me try to guess in as few attempts as possible.\n')

input('Press ENTER when ready to play.\n')

# set variables
clue = ''
count_guess = 0
low_range = -1
high_range = 101


while clue != 'correct' and low_range != high_range:


number = random.randint(low_range + 1, high_range - 1)
	
count_guess += 1

print('\nIs your number', number, end= '? ')
clue = input('\n\nEnter one of the following clues:\n\n' \
+ '\t\thigher\n'
+ '\t\tlower\n'
+ '\t\tcorrect\n\n'
+ 'Type here and press ENTER: ')

if clue == 'lower':
 high_range = number			

elif clue == 'higher':
 low_range = number

elif clue == 'correct':

if count_guess > 1:
 print('\nYou picked', number,'and it took me only', count_guess, 'attempts to find out.')

else: print('\nI could read your mind at once and saw you picked', number)


if low_range == high_range:
print('\nYou picked', number,'and it took me only', count_guess, 'attempts to find out.')


Python 3.3.2+ (default, Oct  9 2013, 14:50:09) 
[GCC 4.8.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>>  RESTART 
>>> 
Pick a number between and 0 and 100...
...and let me try to guess in as few attempts as possible.

Press ENTER when ready to play.


Is your number 92? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: higher

Is your number 93? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: higher

Is your number 97? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: lower

Is your number 95? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: higher

Is your number 96? 

Enter one of the following clues:

		higher
		lower
		correct

Type here and press ENTER: lower


Traceback (most recent call last):
  File "/home/marc/Ubuntu One/Python/machine_guess_number.py", line 22, in 
number = random.randint(low_range + 1, high_range - 1)
  File "/usr/lib/python3.3/random.py", line 214, in randint
r

[Tutor] Beginner - list not returning correct variable value

2014-03-13 Thread Marc Eymard
Hello Tutor,

I am a self-taught Python script beginner and I do it from the Michael Dawson 
Book.

In attached script, can somebody tell me why the values of the variables 
strenght_points, health_points, wisdom_points and dexterity_points stay at 0 
value when 'printing' their value from the list attributes[] when the 'for' 
loop at the bottom of the script runs.

I have commented out the script to highlight what causes me problem.

I hope this is clear enough a question.

Thanks for your help,
Marc
  #Character role play
#you have 4 attributes and can assign/remove points to/from it
#use short story scripting to task with no MENU tree
#ignore calculus logic limitations

#set variables
strength_points = 0
health_points = 0
wisdom_points = 0
dexterity_points = 0
pool = 30
att_choice = None

attributes = [('strength',strength_points),
  ('health',health_points),
  ('wisdom',wisdom_points),
  ('dexterity',dexterity_points)]



print('Hello gladiator,\n')

print('you have the following attributes with no points:\n')

for i in attributes:
att_name, points = i
print(att_name,'\t',points)

input('\nPress ENTER to continue')

print('\nYou are now entrusted with a pool of 30 points to spend on them as you whish')
input('Press ENTER to continue')

print('\nUsing negative points will move them back to your pool of points')
input('Press ENTER to continue')

print('\nChose an attribute and add/remove points at your will:   ')

while att_choice != '0':

#here proof that variables values are changing as expexted:
print('here proof that variables values are changing as expected:')
print(strength_points,health_points,wisdom_points,dexterity_points,pool)


print('\n[1] stength [2] health [3] wisdom [4] dexterity [0] EXIT')
att_choice = input('ENTER number here: ')

if att_choice == '1':
  print('\nHow many strenght points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  strength_points += points
  pool -= points

elif att_choice == '2':
  print('\nHow many health points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  health_points += points
  pool -= points

elif att_choice == '3':
  print('\nHow many wisdom points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  wisdom_points += points
  pool -= points

elif att_choice == '4':
  print('\nHow many dexterity points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  dexterity_points += points
  pool -= points

elif att_choice == '0':
print('Good Bye Gladiator!')
break

else:
print('Sorry I don\'t understand your selection')
input('Press ENTER to try again')

print('\nYou have now the following attributes and points:\n')

#here the loop does not return variable value as expected:
for i in attributes:
att_name, point = i
print(att_name,'\t',point)

input('\nPress ENTER to continue\n')

input('\nPress ENTER to exit')


  

  



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


[Tutor] FW: Beginner - list not returning correct variable value

2014-03-19 Thread Marc Eymard
Hello Tutor,

Could somebody help me with below query?

Thanks
Marc

From: marc_eym...@hotmail.com
To: tutor@python.org
Subject: Beginner - list not returning correct variable value
Date: Thu, 13 Mar 2014 16:12:28 +




Hello Tutor,

I am a self-taught Python script beginner and I do it from the Michael Dawson 
Book.

In attached script, can somebody tell me why the values of the variables 
strenght_points, health_points, wisdom_points and dexterity_points stay at 0 
value when 'printing' their value from the list attributes[] when the 'for' 
loop at the bottom of the script runs.

I have commented out the script to highlight what causes me problem.

I hope this is clear enough a question.

Thanks for your help,
Marc

  #Character role play
#you have 4 attributes and can assign/remove points to/from it
#use short story scripting to task with no MENU tree
#ignore calculus logic limitations

#set variables
strength_points = 0
health_points = 0
wisdom_points = 0
dexterity_points = 0
pool = 30
att_choice = None

attributes = [('strength',strength_points),
  ('health',health_points),
  ('wisdom',wisdom_points),
  ('dexterity',dexterity_points)]



print('Hello gladiator,\n')

print('you have the following attributes with no points:\n')

for i in attributes:
att_name, points = i
print(att_name,'\t',points)

input('\nPress ENTER to continue')

print('\nYou are now entrusted with a pool of 30 points to spend on them as you whish')
input('Press ENTER to continue')

print('\nUsing negative points will move them back to your pool of points')
input('Press ENTER to continue')

print('\nChose an attribute and add/remove points at your will:   ')

while att_choice != '0':

#here proof that variables values are changing as expexted:
print('here proof that variables values are changing as expected:')
print(strength_points,health_points,wisdom_points,dexterity_points,pool)


print('\n[1] stength [2] health [3] wisdom [4] dexterity [0] EXIT')
att_choice = input('ENTER number here: ')

if att_choice == '1':
  print('\nHow many strenght points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  strength_points += points
  pool -= points

elif att_choice == '2':
  print('\nHow many health points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  health_points += points
  pool -= points

elif att_choice == '3':
  print('\nHow many wisdom points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  wisdom_points += points
  pool -= points

elif att_choice == '4':
  print('\nHow many dexterity points you want to add (+) or remove (-) ?')
  points = int(input('Enter points here: '))
  dexterity_points += points
  pool -= points

elif att_choice == '0':
print('Good Bye Gladiator!')
break

else:
print('Sorry I don\'t understand your selection')
input('Press ENTER to try again')

print('\nYou have now the following attributes and points:\n')

#here the loop does not return variable value as expected:
for i in attributes:
att_name, point = i
print(att_name,'\t',point)

input('\nPress ENTER to continue\n')

input('\nPress ENTER to exit')


  

  



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


[Tutor] Thread Object integration with GPIO

2017-04-29 Thread Marc Eymard
Hello there,

I have hooked up an ultrasonic sensor to my Raspberry Pi-enabled robot 
in order to get continuous distance-to-obstacle reading.

The sensor is properly connected via GPIO and already reads the distance 
properly when running a simple script.

However, I need to integrate the sensor reading logic to an existing 
script provided by PiBorg called YetiBorg.py

The way I have decided to go about implementing the sensor reading is by 
creating a Thread object and update the distance attribute of this very 
same object from the run() function. The idea is to encapsulate the 
distance reading within the sensor object as much as possible by i. 
creating a sensor/thread object and by ii. avoiding global variables.

Attached the script I have come up with, which keeps returning multiple 
run time errors whenever I try to fix it.
I believe there are multiple issues, but at least it gives an idea of 
what I currently want to achieve and how.

Can a charitable soul advise whether my approach makes sense and whether 
I am using the right type of object for the task at hands?

I am looking for guidance and willing try completely different 
approach/objects if necessary.

Thanks in advance for sending me into the right direction.
Marc

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


[Tutor] Thread Object integration with GPIO

2017-04-30 Thread Marc Eymard
Hello there,

I have hooked up an ultrasonic sensor to my Raspberry Pi-enabled robot
in order to get continuous distance-to-obstacle reading.

The sensor is properly connected via GPIO and already reads the distance
properly when running a simple script.

However, I need to integrate the sensor reading logic to an existing
script provided by PiBorg called YetiBorg.py

The way I have decided to go about implementing the sensor reading is by
creating a Thread object and update the distance attribute of this very
same object from the run() function. The idea is to encapsulate the
distance reading within the sensor object as much as possible by i.
creating a sensor/thread object and by ii. avoiding global variables.

Below the script I have come up along with the error print.
I believe there are multiple issues, but at least it gives an idea of
what I currently want to achieve and how.

Can a charitable soul advise whether my approach makes sense and whether
I am using the right type of object for the task at hands?

I am looking for guidance and willing try completely different
approach/objects if necessary.

Thanks in advance for sending me into the right direction.
Marc

Python Shell

2.7.9

OS-

Raspian Pixel on Raspberry Pi Zero



---traceback



Traceback (most
recent call last):

File
"/home/pi/Desktop/distance sensor class object.py", line
57, in 


SensorA = Sensor(interval=1, gpio_trig=23, gpio_echo=24)

File"/home/pi/Desktop/distance sensor class object.py", line
23, in __init__self.start()


RuntimeError:
thread.__init__() not called





--sensor.py-


import threading
import RPi.GPIO as GPIO
import time

#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)

class Sensor(threading.Thread):

"""ultrasonic sensor continous distance reading at given interval in 
seconds"""

def __init__(self,interval, gpio_trig, gpio_echo):
self.inter = interval
self.trig = gpio_trig
self.echo = gpio_echo

#set GPIO pins direction (IN / OUT)
GPIO.setup(gpio_trig, GPIO.OUT)
GPIO.setup(gpio_echo, GPIO.IN)

self.dist = 0
self.terminated = False
self.start()

def run(self):
while not self.terminated:
# set Trigger to HIGH
GPIO.output(gpio_trig, True)

# set Trigger to LOW after 0.01ms
time.sleep(0.1)
GPIO.output(gpio_trig, False)

StartTime = time.time()
StopTime = time.time()

# save StartTime
while GPIO.input(gpio_echo) == 0:
StartTime = time.time()

# save time of arrival
while GPIO.input(gpio_echo) == 1:
StopTime = time.time()

# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply by sonic speed (34300 cm/s)
# and divide by 2, because there and back
self.dist = (TimeElapsed * 34300) / 2

time.sleep(self.inter)

def get_dist(self):
return self.dist

#Sensor object "instanciated" with GPIO programmable pins 23 and 24
SensorA = Sensor(interval=1, gpio_trig=23, gpio_echo=24)

try:
while True:
print("Measured Distance = %.1f cm" % SensorA.get_dist())
except KeyboardInterrupt:
GPIO.cleanup()
SensorA.terminated = True



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