[Tutor] (no subject)

2019-03-16 Thread Glenn Dickerson
class Student():

def__init__(self, name, major, gpa, is_on_probation):
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation


import Student
student1 = Student('Jim', 'Business', 3.1, False)
student2 = Student('Pam', 'Art', 2.5, True)
print(student1.name)
print(student2.gpa)

I entered this in IDLE and it failed. Any advice? Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2019-03-16 Thread Steven D'Aprano
Hi Glenn, and welcome.

On Fri, Mar 15, 2019 at 09:54:41PM -0400, Glenn Dickerson wrote:
> class Student():
> def__init__(self, name, major, gpa, is_on_probation):
> self.name = name
> self.major = major
> self.gpa = gpa
> self.is_on_probation = is_on_probation
> 
> 
> import Student
> student1 = Student('Jim', 'Business', 3.1, False)
> student2 = Student('Pam', 'Art', 2.5, True)
> print(student1.name)
> print(student2.gpa)
> 
> I entered this in IDLE and it failed. Any advice? Thank you.

Yes -- read the error message. What does it say?


(Reading, and understanding, error messages is probably the most 
important skill a programmer can have.)



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


Re: [Tutor] (no subject)

2019-03-16 Thread Alan Gauld via Tutor
On 16/03/2019 01:54, Glenn Dickerson wrote:
> class Student():
> 
> def__init__(self, name, major, gpa, is_on_probation):
> self.name = name
> self.major = major
> self.gpa = gpa
> self.is_on_probation = is_on_probation
> 
> 
> import Student
> student1 = Student('Jim', 'Business', 3.1, False)
> student2 = Student('Pam', 'Art', 2.5, True)
> print(student1.name)
> print(student2.gpa)
> 
> I entered this in IDLE and it failed. Any advice? Thank you.

Please, never just say "it failed".
How did it fail?
Did you get an error message? If so, send us the complete message
Did IDLE crash?
Did the PC crash?
Did it run with no output?
Did it run with the wrong output?

There are so many ways for a program to "fail" we need
more specific details.

In this case I can guess what might have happened.
I suspect you need to read up on how to import and
use an external file, but again there are at least
3 possible errors that you might have and I can't
tell which from the details you provided.


-- 
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] (no subject)

2019-03-16 Thread Peter Otten
Glenn Dickerson wrote:

> class Student():
> 
> def__init__(self, name, major, gpa, is_on_probation):
> self.name = name
> self.major = major
> self.gpa = gpa
> self.is_on_probation = is_on_probation
> 
> 
> import Student
> student1 = Student('Jim', 'Business', 3.1, False)
> student2 = Student('Pam', 'Art', 2.5, True)
> print(student1.name)
> print(student2.gpa)
> 
> I entered this in IDLE and it failed. Any advice?

First of all, try to be as precise as you can with your error descriptions.
What was the exact error, what looked the traceback like?

Do not retype, use cut-and-paste to put them into your post.

Was it something like

  File "Student.py", line 3
def__init__(self, name, major, gpa, is_on_probation):
^
SyntaxError: invalid syntax

Then the problem is the missing space between the keyword "def" and the 
method name "__init__".

Or was it

Traceback (most recent call last):
  File "student.py", line 10, in 
import Student
ImportError: No module named 'Student'

That's because Student is a class rather than a module, and you neither
need to nor can import it directly. If you remove the import statement your 
code should work.

But wait, there's another option. If you saw

Traceback (most recent call last):
  File "Student.py", line 10, in 
import Student
  File "Student.py", line 11, in 
student1 = Student('Jim', 'Business', 3.1, False)
TypeError: 'module' object is not callable

then a "Student" module was imported successfully. However, as it has the 
same name as your class, the name "Student" is now bound to the module, and 
unlike the class a module cannot be called. The solution then is to rename 
the module, from Student.py to student.py, say, as lowercase module names 
are the preferred convention anyway.

So there are at least three possible problems in your tiny and almost 
correct code snippet. 

In a script that does some actual work the number of possible problems 
explodes, and that's why most of us don't even start to debug a piece of 
code without a detailed error description and a traceback -- it's usually a 
waste of time.


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


Re: [Tutor] Question for tutoring page

2019-03-16 Thread Albert-Jan Roskam



On 13 Mar 2019 18:14, Alan Gauld via Tutor  wrote:

On 11/03/2019 16:10, Diana Katz wrote:
> What is the best way to ..program using python - that could recognize
> a 3D object and then rank drawings of the object as to which are more
> accurate.

===>> check this out: 
https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html

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


Re: [Tutor] Remove soft line break

2019-03-16 Thread Valerio Pachera


- Messaggio originale -
> Da: "Valerio Pachera" 
> A: "Tutor Python" 
> Inviato: Giovedì, 28 febbraio 2019 13:05:27
> Oggetto: Re: [Tutor] Remove soft line break

> ...
> I noticed that the end of file doesn't get preserve if I create a copy of the
> file ...

I've been told by a collegue that the when the file is opened by interpreter it 
uses the end of line of the system the program runs on.
In my case the os is linux, so python uses '\n' a end of line, no matter what's 
written in the file.
The end of line, ore better "newline" may be chosen when open the file.

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, 
closefd=True, opener=None)

So, if I have this string

s = '''
hallo, this is 
a multi line text
'''

I can save it in a text file for windows this way:

f = open('test.txt', 'w', newline='\r\n')
f.write(s)
f.close()

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


[Tutor] Merge a dictionary into a string

2019-03-16 Thread Valerio Pachera
Consider this:

import collections
d = OrderedDict(a='hallo', b='world')

I wish to get a single string like this:

'a "hallo" b "world"'

Notice I wish the double quote to be part of the string.
In other words I want to wrap the value of a and b.

I was thinking to use such function I created:

def mywrap(text, char='"'):
return(char + text + char)

I can't think anything better than

s = ''
for k, v in d.items():
 s += ' '.join( (k, mywrap(v)) ) + ' '

or

s = ''
for k, v in d.items():
s += k + ' ' + mywrap(v) + ' '

What do you think?
It's fine enough but I wonder if there's a better solution.

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


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Alan Gauld via Tutor
On 16/03/2019 17:39, Valerio Pachera wrote:

> I wish to get a single string like this:
> 
> 'a "hallo" b "world"'
> 
> Notice I wish the double quote to be part of the string.
> In other words I want to wrap the value of a and b.

When dealing with string layouts I tend to go to
string formatting...

>>> d= {'a':"alpha",'b':"beta"}
>>> ' '.join(['{} "{}"'.format(k,v) for k,v in d.items()])
'a "alpha" b "beta"'
>>>

Or using old C style formatting, it's very slightly shorter:

>>> ' '.join(['%s "%s"'% (k,v) for k,v in d.items()])
'a "alpha" b "beta"'

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


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Peter Otten
Valerio Pachera wrote:

> Consider this:
> 
> import collections
> d = OrderedDict(a='hallo', b='world')
> 
> I wish to get a single string like this:
> 
> 'a "hallo" b "world"'
> 
> Notice I wish the double quote to be part of the string.
> In other words I want to wrap the value of a and b.
> 
> I was thinking to use such function I created:
> 
> def mywrap(text, char='"'):
> return(char + text + char)
> 
> I can't think anything better than
> 
> s = ''
> for k, v in d.items():
>  s += ' '.join( (k, mywrap(v)) ) + ' '
> 
> or
> 
> s = ''
> for k, v in d.items():
> s += k + ' ' + mywrap(v) + ' '
> 
> What do you think?
> It's fine enough but I wonder if there's a better solution.

In Python 3.6 and above you can use f-strings:

>>> d = dict(a="hello", b="world")
>>> " ".join(f'{k} "{v}"' for k, v in d.items())
'a "hello" b "world"'

By the way, are you sure that the dictionary contains only strings without 
spaces and '"'?

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


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Alex Kleider

On 2019-03-16 10:39, Valerio Pachera wrote:

Consider this:

import collections
d = OrderedDict(a='hallo', b='world')

I wish to get a single string like this:

'a "hallo" b "world"'

Notice I wish the double quote to be part of the string.
In other words I want to wrap the value of a and b.

I was thinking to use such function I created:

def mywrap(text, char='"'):
return(char + text + char)

I can't think anything better than

s = ''
for k, v in d.items():
 s += ' '.join( (k, mywrap(v)) ) + ' '

or

s = ''
for k, v in d.items():
s += k + ' ' + mywrap(v) + ' '

What do you think?
It's fine enough but I wonder if there's a better solution.



Would the following not  give you what you want:
(I've not used OrderedDict but I believe it would work for dict so 
assume ok for OrderedDict.)


my_which_string = "a = '{a}'  b = '{b}'".format(**d)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Merge a dictionary into a string

2019-03-16 Thread Alan Gauld via Tutor
On 16/03/2019 18:44, Peter Otten wrote:
>
> In Python 3.6 and above you can use f-strings:
> 
 d = dict(a="hello", b="world")
 " ".join(f'{k} "{v}"' for k, v in d.items())
> 'a "hello" b "world"'

Cool, I'd missed f-strings. Time for some reading

Thanks Peter,


-- 
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] Merge a dictionary into a string

2019-03-16 Thread Mats Wichmann
On March 16, 2019 5:57:23 PM MDT, Alan Gauld via Tutor  wrote:
>On 16/03/2019 18:44, Peter Otten wrote:
>>
>> In Python 3.6 and above you can use f-strings:
>> 
> d = dict(a="hello", b="world")
> " ".join(f'{k} "{v}"' for k, v in d.items())
>> 'a "hello" b "world"'
>
>Cool, I'd missed f-strings. Time for some reading
>
>Thanks Peter,

f-strings are great, but a lot of people have to support multiple python 
versions so they're not a big option for everyone... yet.
-- 
Sent from a mobile device with K-9 Mail. Please excuse my brevity.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor