[Tutor] Fwd: Re: fractions from Fractions

2018-02-06 Thread Alan Gauld via Tutor
Forwarding to list, please always use Reply ALL or Reply LIst to send
mail to the list.



-Original Message-
From: Alan Gauld via Tutor 
To: tutor 
Sent: Mon, Feb 5, 2018 4:50 am
Subject: Re: [Tutor] fractions from Fractions

On 05/02/18 04:11, Rex Florian via Tutor wrote:

> The problem is one of the PyCharm problems and when I
> use the check feature it tells me my answer is incorrect.

What input did you use and what output did you get?

Input 239/30  --->  7 1 29
Input 415/93  --->  4 2 6 7

So far as I can tell your algorithm is correct, although
I'm not sure why you limit it to values greater than 1?

The problem suggests to do so and the wiki on Finite simple continued
fractions showed examples also greater than one.  Upon consideration, i
suppose that being greater than one is not a requirement but really is
not of consequence to the insight I am seeking.

But is it perhaps the format of the output that Pycharm
objects to?

No, the format required is for the elements of the list to appear on one
line separated by a space.

> I think I know the source of the trouble.

Would you like to tell us?

My input is a string which I convert to num and den.  The problem ask
specifically that the input format be "A line with an fraction in the
"numerator/denominator" format and I am interpreting this to mean some
kind of application of the Fraction module.  But that is just a wild
guess and is what I was hoping I could get some insight on.

So, is there away with using the Fraction module to input a fraction
without having to go through the int conversions that my code employees?

> I have tried importing fractions from Fraction

And what happened?

Not much.  I just bungled something:
fraction = input(Fraction(numerator, denominator)
Please don't scold me.  It was late and I was fighting the flu and  
and ...

(I assume you mean you tried importing Fraction from fractions)
Although you don't really need it for this problem.

I agree I do not really need to for this problem but am just trying to
see if I could input a fraction that would not need to be processed like
my program does.

> 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=' ')


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Fwd: Re: fractions from Fractions

2018-02-06 Thread Alan Gauld via Tutor
On 06/02/18 09:26, Alan Gauld via Tutor wrote:
> Forwarding to list, please always use Reply ALL or Reply LIst to send
> mail to the list.
> 

> What input did you use and what output did you get?
> 
> Input 239/30  --->  7 1 29
> Input 415/93  --->  4 2 6 7
> 
> So far as I can tell your algorithm is correct, although
> I'm not sure why you limit it to values greater than 1?
> 
> The problem suggests to do so and the wiki on Finite simple continued
> fractions showed examples also greater than one.  Upon consideration, i
> suppose that being greater than one is not a requirement but really is
> not of consequence to the insight I am seeking.

It depends on how the PyCharm "checker" works.
If it uses a test value less than 1 it will fail the test.

> But is it perhaps the format of the output that Pycharm
> objects to?
> 
> No, the format required is for the elements of the list to appear on one
> line separated by a space.

Do you have the exact wording of the requirements?
Or even a url to the PyCharm site for this specific problem?

>> I think I know the source of the trouble.
> Would you like to tell us?
> 
> My input is a string which I convert to num and den.  The problem ask
> specifically that the input format be "A line with an fraction in the
> "numerator/denominator" format and I am interpreting this to mean some
> kind of application of the Fraction module.  But that is just a wild
> guess and is what I was hoping I could get some insight on.

I don't think so, your conversion is doing the same job.

Personally I'd have used split() rather than finding
the index and slicing

num,den = [int(n) for n in fraction.split('/')]

But under the covers its doing much the same as your code.

>> I have tried importing fractions from Fraction
> 
> And what happened?
> 
> Not much.  I just bungled something:
> fraction = input(Fraction(numerator, denominator)

You would have needed something like

fraction = Fraction(input('>'))

Assuming Fraction has a string based constructor which
I don't think it does. I really don't think Fraction
helps you here.

>> while num > den and den != 0:

I'd let it handle numbers less than 1 by converting
the while loop:

while den > 0


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] fix overwriting issue

2018-02-06 Thread renukesh nk
Hi,

i am facing issue while writing files to a folder, where the files get
overwrite if they have same file names , so any help me to  fix

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


Re: [Tutor] fix overwriting issue

2018-02-06 Thread Alan Gauld via Tutor
On 06/02/18 10:34, renukesh nk wrote:
> i am facing issue while writing files to a folder, where the files get
> overwrite if they have same file names , so any help me to  fix

That's just what happens, what did you expect to happen?
The same would be true id you saved a file from notepad,
if you used an existing name notepad would overwrite the
old file (albeit with a warning).

Are you trying to add extra information to the end of an
existing file? If so use the "a" mode when opening the
file instead of "w".

If the problem is accidentally typing inan existing name
then you can use the os module to test whether the file
exists before opening it and giving a warning if it does.

But until we know more about what you are trying to do
we can't really offer much more help. Also it helps if
you show us the code - at lest the bit that opens the
file.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Fwd: Re: fractions from Fractions

2018-02-06 Thread Wolfgang Maier

On 06.02.2018 10:53, Alan Gauld via Tutor wrote:


You would have needed something like

fraction = Fraction(input('>'))

Assuming Fraction has a string based constructor which
I don't think it does. I really don't think Fraction
helps you here.


Oh, most certainly it has - though it may accept more forms of input 
than the OP needs/wants:


>>> from fractions import Fraction
>>> Fraction('2/3')
Fraction(2, 3)
>>> Fraction('2.3')
Fraction(23, 10)
>>> Fraction('2e-3')
Fraction(1, 500)
>>>

Since fractions is pure Python it may also be instructive to study its 
source:

https://github.com/python/cpython/blob/master/Lib/fractions.py

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


Re: [Tutor] EXTERNAL: fix overwriting issue

2018-02-06 Thread Joaquin Henriquez
Hi 

This is a python forum and we try to help as much as possible.

It wpuld be usefull from your side to put the python code you are trying to run 
an explain whats is wrong and what you are trying to do.

BR



-Original Message-
From: Tutor [mailto:tutor-bounces+joaquin.henriquez=countercept@python.org] 
On Behalf Of renukesh nk
Sent: 06 February 2018 10:35
To: tutor@python.org
Subject: EXTERNAL: [Tutor] fix overwriting issue

Hi,

i am facing issue while writing files to a folder, where the files get
overwrite if they have same file names , so any help me to  fix

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

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


[Tutor] What is day of week from either 20180211 or 02112018

2018-02-06 Thread Ken Green

Greeting: I have been trying to determine the day of
the week when inputting year + month + date. I have
not yet been able to determine what is really needed
for datetime and later on the date in the program below.
Running Python 2.7 on Ubuntu 16.04. Thanks.
===
# A_Weekday.py
from datetime import date
year = "2018"
monthdate = raw_input ("Enter the month and date (MDD or MMDD): ")
print
if (len(monthdate)) == 3:
    month = monthdate[0:1]
    month = "0" + month
    day  = monthdate[1:3]
else:
    month = monthdate[0:2]
    day  = monthdate[2:4]
print month, day, year; print
print year, month, day; print
datecode = year + month + day
print datecode
print
answer = datetime.date(year, month, day).weekday()
print answer
==
Error message below:
Enter the month and date (MDD or MMDD): 211
02 11 2018
2018 02 11
20180211
Traceback (most recent call last):
  File "A_Weekday.py", line 20, in 
    answer = datetime.date(year, month, day).weekday()
NameError: name 'datetime' is not defined

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


Re: [Tutor] What is day of week from either 20180211 or 02112018

2018-02-06 Thread David Rock

> On Feb 6, 2018, at 13:45, Ken Green  wrote:
> Traceback (most recent call last):
>   File "A_Weekday.py", line 20, in 
> answer = datetime.date(year, month, day).weekday()
> NameError: name 'datetime' is not defined

Your error message tells you the problem.  You are importing date _from_ 
datetime, but then try to call datetime.date

> from datetime import date

You have _not_ imported date time, so the program doesn’t know what you mean 
when you say

answer = datetime.date(year, month, day).weekday()

Try just doing 

import datetime 

instead.


— 
David Rock
da...@graniteweb.com




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


Re: [Tutor] What is day of week from either 20180211 or 02112018

2018-02-06 Thread Mark Lawrence

On 06/02/18 19:45, Ken Green wrote:

Greeting: I have been trying to determine the day of
the week when inputting year + month + date. I have
not yet been able to determine what is really needed
for datetime and later on the date in the program below.
Running Python 2.7 on Ubuntu 16.04. Thanks.
===
# A_Weekday.py
from datetime import date
year = "2018"
monthdate = raw_input ("Enter the month and date (MDD or MMDD): ")
print
if (len(monthdate)) == 3:
     month = monthdate[0:1]
     month = "0" + month
     day  = monthdate[1:3]
else:
     month = monthdate[0:2]
     day  = monthdate[2:4]
print month, day, year; print
print year, month, day; print
datecode = year + month + day
print datecode
print
answer = datetime.date(year, month, day).weekday()
print answer
==
Error message below:
Enter the month and date (MDD or MMDD): 211
02 11 2018
2018 02 11
20180211
Traceback (most recent call last):
   File "A_Weekday.py", line 20, in 
     answer = datetime.date(year, month, day).weekday()
NameError: name 'datetime' is not defined



There's no need for the `datetime` as you've imported `date` directly 
into the namespace.  Correct that problem and you'll still fail as 
you're passing strings instead of integers.  I'd just force your 
`monthdate` to be MMDD and pass that into `strptime` with `year`.


>>> year = '2018'
>>> monthdate = '0211' # skipping the input code.
>>> from datetime import datetime
>>> datetime.strptime(year + monthdate, '%Y%m%d')
datetime.datetime(2018, 2, 11, 0, 0)
>>> datetime.strptime(year + monthdate, '%Y%m%d').weekday()
6


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] What is day of week from either 20180211 or 02112018 (SOLVED)

2018-02-06 Thread Ken Green



On 02/06/2018 03:20 PM, David Rock wrote:

On Feb 6, 2018, at 13:45, Ken Green  wrote:
Traceback (most recent call last):
   File "A_Weekday.py", line 20, in 
 answer = datetime.date(year, month, day).weekday()
NameError: name 'datetime' is not defined

Your error message tells you the problem.  You are importing date _from_ 
datetime, but then try to call datetime.date


from datetime import date

You have _not_ imported date time, so the program doesn’t know what you mean 
when you say

answer = datetime.date(year, month, day).weekday()

Try just doing

import datetime

instead.


—
David Rock
da...@graniteweb.com



Thank you, sir. You put me on the right path. Much appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is day of week from either 20180211 or 02112018 (SOLVED)

2018-02-06 Thread Ken Green



On 02/06/2018 03:36 PM, Mark Lawrence wrote:

On 06/02/18 19:45, Ken Green wrote:

Greeting: I have been trying to determine the day of
the week when inputting year + month + date. I have
not yet been able to determine what is really needed
for datetime and later on the date in the program below.
Running Python 2.7 on Ubuntu 16.04. Thanks.
===
# A_Weekday.py
from datetime import date
year = "2018"
monthdate = raw_input ("Enter the month and date (MDD or MMDD): ")
print
if (len(monthdate)) == 3:
 month = monthdate[0:1]
 month = "0" + month
 day  = monthdate[1:3]
else:
 month = monthdate[0:2]
 day  = monthdate[2:4]
print month, day, year; print
print year, month, day; print
datecode = year + month + day
print datecode
print
answer = datetime.date(year, month, day).weekday()
print answer
==
Error message below:
Enter the month and date (MDD or MMDD): 211
02 11 2018
2018 02 11
20180211
Traceback (most recent call last):
   File "A_Weekday.py", line 20, in 
 answer = datetime.date(year, month, day).weekday()
NameError: name 'datetime' is not defined



There's no need for the `datetime` as you've imported `date` directly 
into the namespace.  Correct that problem and you'll still fail as 
you're passing strings instead of integers.  I'd just force your 
`monthdate` to be MMDD and pass that into `strptime` with `year`.


>>> year = '2018'
>>> monthdate = '0211' # skipping the input code.
>>> from datetime import datetime
>>> datetime.strptime(year + monthdate, '%Y%m%d')
datetime.datetime(2018, 2, 11, 0, 0)
>>> datetime.strptime(year + monthdate, '%Y%m%d').weekday()
6


Thanks. I already got it up and running. Much appreciated.

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