[Tutor] Class

2017-06-21 Thread Rex Florian via Tutor

Hello,

Below is a class I am using to comprehend how class works.  
The code came from tutorialspoint.com and executes correctly but I do not 
understand why it works.
The original example defined just v1 and v2.  I decided to experiment and 
instantiated v3.
The executed the print statement yields a correct answer which baffles me as to 
how.
I also tried (v1 + v2 + v3 + v1) which works as well.

Can someone explain how Python achieves the vector addition of more than 2 
vectors
without some kind of looping?

class Vector:
   def __init__(self, a, b):
  self.a = a
  self.b = b

   def __str__(self):
  return 'Vector (%d, %d)' % (self.a, self.b)

   def __add__(self,other):
  return Vector(self.a + other.a, self.b + other.b)

v1 = Vector(2,10)
v2 = Vector(5,-2)
v3 = Vector(16,-14)
print(v1 + v2 + v3)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] fractions from Fractions

2018-02-05 Thread Rex Florian via Tutor
I have written a program to generate the array that describes the finite simple 
continued fraction from any fractional input.  The input is any fraction, 
num/den, and the output is the array.  The program works and I am happy with 
the work.

The problem is one of the PyCharm problems and when I use the check feature it 
tells me my answer is incorrect.  I think I know the source of the trouble.  
The problem asks that my input format be "A single line with an fraction in the 
numerator/denominator format.

My input is obtained as seen in the code.  I have googled fractions in Python.  
I have tried importing fractions from Fraction but seem to be in over my head.  
What am I missing?



fraction = input()
# define list for holding coefficients
coef = []

# find the index where the / is
slash = fraction.find('/')
# extract the string num and den and convert them to integers
num = int(fraction[0:slash])
den = int(fraction[slash + 1:])

while num > den and den != 0:
mod = num % den
a = num // den
coef.append(a)
num = den
den = mod
for i in coef:
print(i, end=' ')


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


[Tutor] running a .py file from the comand line

2018-04-02 Thread Rex Florian via Tutor
Hello,

I am running Python 3.6 in a Window 7 environment.  I have a python script that 
I am trying to run from the command line.

The script is from a Learning to Program file demonstrating event driven 
programming.  I have copied it to a file named Ascii Keys.py into my user 
directory c:\Users\Rex

I try to execute the file by typing python Ascii Keys.py at the command line 
and receive the following message:

python:  can't open file 'Ascii':  [errno2] no such file or directory

I expected the python .py file to open and run.

I check with the path command and receive among other paths these to paths to 
Python.

c:\Users\Rex/AppData\Local\Programs\Python\Python36-32\Scripts

c:\Users\Rex/AppData\Local\Programs\Python\Python36-32

I also load python from c:\Users\Rex and try running Ascii Keys from >> and get 
the following error:

File "", line 1
   Ascii Keys

SyntaxError:  invalid syntax

Why doesn't python open my file?

Also, in windows explorer, I tried to find the above paths and could not see 
AppData under c:\Users\Rex

What am I missing here?

import msvcrt
import sys

# First the event handlers
def doKeyEvent(key):
if key == '\x00' or key == '\xe0':
   key = msvcrt.getch()
print ( ord(key), ' ', end='')
sys.stdout.flush() # make sure it appears on screen

def doQuit(key):
print() # force a new line
raise SystemExit

# first clear some screen space
lines = 25 
for n in range(lines): print()

# Now the main event-loop
while True:
ky = msvcrt.getch()
if len(str(ky)) != 0:
# we have a real event
if " " in str(ky):
doQuit(ky)
else: 
doKeyEvent(ky)

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