[Tutor] taking a tuple with input
Hey Guys, i'm running Python 3.3.2 on Windows 7 64 Bit I am writing a little script for practice and got a little problem. I wrote a class which got two points in the constructor (p1 and p2). With the function distanceOf from my class i measure the distance between these two points. Everything works fine. But when i want to use input to get the points i does not work... So how can i get an int tuple with input? Some Code: class points: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def distanceOf(self): diff = (self.p2[0] - self.p1[0], self.p2[1] - self.p1[1]) a = diff[0] b = diff[1] result = math.sqrt(a**2 + b**2) return "The distance between the two points:", round(result) When i type test = points((25.0, 30.0), (40.0, 55.0)) and test.distanceOf() everything is ok. Now i wont to get input. (In the input prompt i write: (25.0, 30.0) p1 = input('Please type in some coordinates') p2 = input('Please type in some coordinates') test = points(p1, p2) points.distanceOf() Traceback (most recent call last): File "lines.py", line 16, in line.distanceOf() File "lines.py", line 6, in distanceOf diff = (self.p2[0] - self.p1[0], self.p2[1] - self.p1[1]) TypeError: unsupported operand type(s) for -: 'str' and 'str' i get this error can anyone help me out? How can i get an tuple with int values from user input? Greetings ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] taking a tuple with input
On 26/10/2013 04:09, Sven Hennig wrote: Hey Guys, i'm running Python 3.3.2 on Windows 7 64 Bit I am writing a little script for practice and got a little problem. I wrote a class which got two points in the constructor (p1 and p2). With the function distanceOf from my class i measure the distance between these two points. Everything works fine. But when i want to use input to get the points i does not work... So how can i get an int tuple with input? Some Code: class points: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def distanceOf(self): diff = (self.p2[0] - self.p1[0], self.p2[1] - self.p1[1]) a = diff[0] b = diff[1] result = math.sqrt(a**2 + b**2) return "The distance between the two points:", round(result) When i type test = points((25.0, 30.0), (40.0, 55.0)) and test.distanceOf() everything is ok. Now i wont to get input. (In the input prompt i write: (25.0, 30.0) p1 = input('Please type in some coordinates') p2 = input('Please type in some coordinates') test = points(p1, p2) points.distanceOf() Traceback (most recent call last): File "lines.py", line 16, in line.distanceOf() File "lines.py", line 6, in distanceOf diff = (self.p2[0] - self.p1[0], self.p2[1] - self.p1[1]) TypeError: unsupported operand type(s) for -: 'str' and 'str' i get this error can anyone help me out? How can i get an tuple with int values from user input? Greetings Twp options from the top of my head. Either enter the coordinates separately and build your tuples (or lists) before passing them to your points class, or use the ast module literal_eval call. Note that if you choose the latter, you don't need to enter the parentheses, it's the comma that makes the tuple. -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] taking a tuple with input
On 26/10/13 04:09, Sven Hennig wrote: So how can i get an int tuple with input? You can't. input() reads strings. (assuming Python v3) You need to convert the string yourself. For simple floats you just call float() but there isn't an equivalent conversion function for tuples. You probably want to split the string by commas and then convert each part to a float. Something like instr = input('Enter a point (x,y): ') inList = [float(n) for n in instr.split(',')] # check for parens too? point = tuple(inList] You can add extra checks or combine more lines but something along those lines should work. The bigget problem is likely to be getting users to enter the correct format. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] Python and memory allocation
On Thu, Oct 24, 2013 at 05:21:56PM +, Dave Angel wrote: > If you > want a surprise, try the following simple program some time. > > import sys > print(sys.modules) > > when I tried that interactively on 2.7, it printed some 240+ names. Wow. What were you doing? Ah, I bet you had imported numpy or similar! numpy brings in a lot. [steve@ando ~]$ python2.7 -c "import sys, numpy; print len(sys.modules)" 233 Here are the results you get with a freshly-started Python interpreter on Linux, for various versions of Python, excluding numpy. Starting with ancient Python 1.5, and going right up the most recent 3.4 alpha version. [steve@ando ~]$ python1.5 -c "import sys; print len(sys.modules)" 12 [steve@ando ~]$ python2.4 -c "import sys; print len(sys.modules)" 29 [steve@ando ~]$ python2.5 -c "import sys; print len(sys.modules)" 27 [steve@ando ~]$ python2.6 -c "import sys; print len(sys.modules)" 30 [steve@ando ~]$ python2.7 -c "import sys; print len(sys.modules)" 39 [steve@ando ~]$ python3.2 -c "import sys; print(len(sys.modules))" 52 [steve@ando ~]$ python3.3 -c "import sys; print(len(sys.modules))" 54 [steve@ando ~]$ python3.4 -c "import sys; print(len(sys.modules))" 34 And a few others: steve@orac:~$ jython -c "import sys; print len(sys.modules)" 31 steve@orac:~$ ipy -c "import sys; print len(sys.modules)" 21 steve@orac:~$ ipython -c "import sys; print len(sys.modules)" 269 ipython also brings in a lot of modules, even more than numpy. Oooh, now there's a thought! steve@orac:~$ ipython -c "import sys, numpy; print len(sys.modules)" 397 -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and memory allocation
On 26/10/2013 13:20, Steven D'Aprano wrote: On Thu, Oct 24, 2013 at 05:21:56PM +, Dave Angel wrote: If you want a surprise, try the following simple program some time. import sys print(sys.modules) when I tried that interactively on 2.7, it printed some 240+ names. Wow. What were you doing? Ah, I bet you had imported numpy or similar! numpy brings in a lot. This reminded me that work has been done to reduce the number of imports at startup, see http://bugs.python.org/issue19205 and http://bugs.python.org/issue19325 -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] why does platform.architecture default to sys.executable?
Hi, Why does the "executable" parameter default to sys.executable? Yesterday I was surprised to see platform.architecture return "32bit" on a 64-bit system, just because a 32-bit Python interpreter was installed. Wouldn't this make more sense: import sys, platform pf = sys.platform.lower()[:3] executable = "iexplore.exe" if pf[:3] == "win" else "/bin/ls" arch = platform.architecture(executable)[0] Regards, Albert-Jan ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why does platform.architecture default to sys.executable?
On Sun, Oct 27, 2013 at 2:39 AM, Albert-Jan Roskam wrote: > Hi, > > Why does the "executable" parameter default to sys.executable? Yesterday I > was surprised to see platform.architecture return "32bit" on a 64-bit > system, just because a 32-bit Python interpreter was installed. Wouldn't > this make more sense: > > import sys, platform > pf = sys.platform.lower()[:3] > executable = "iexplore.exe" if pf[:3] == "win" else "/bin/ls" I think it's mainly because of avoiding choosing arbitrary programs, although they are most certainly guaranteed to be present. Besides, there are better ways to find the platform architecture, I think. os.uname() comes to mind. -Amit. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why does platform.architecture default to sys.executable?
On Oct 27, 2013 2:51 AM, "Amit Saha" wrote: > > On Sun, Oct 27, 2013 at 2:39 AM, Albert-Jan Roskam wrote: > > Hi, > > > > Why does the "executable" parameter default to sys.executable? Yesterday I > > was surprised to see platform.architecture return "32bit" on a 64-bit > > system, just because a 32-bit Python interpreter was installed. Wouldn't > > this make more sense: > > > > import sys, platform > > pf = sys.platform.lower()[:3] > > executable = "iexplore.exe" if pf[:3] == "win" else "/bin/ls" > > I think it's mainly because of avoiding choosing arbitrary programs, > although they are most certainly guaranteed to be present. Besides, > there are better ways to find the platform architecture, I think. > os.uname() comes to mind. Although that will lie if you have, for example a 32-bit os installed on a 64-bit system. Then, you can read /proc/cpuinfo and look for the lm flag. If it is present, it is a 64-bit system, else a 32-bit one. This is specific to Intel, i think. > > -Amit. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why does platform.architecture default to sys.executable?
--- On Sat, 10/26/13, Amit Saha wrote: Subject: Re: [Tutor] why does platform.architecture default to sys.executable? To: "Albert-Jan Roskam" Cc: "Python Mailing List" Date: Saturday, October 26, 2013, 6:51 PM On Sun, Oct 27, 2013 at 2:39 AM, Albert-Jan Roskam wrote: > Hi, > > Why does the "executable" parameter default to sys.executable? Yesterday I > was surprised to see platform.architecture return "32bit" on a 64-bit > system, just because a 32-bit Python interpreter was installed. Wouldn't > this make more sense: > > import sys, platform > pf = sys.platform.lower()[:3] > executable = "iexplore.exe" if pf[:3] == "win" else "/bin/ls" I think it's mainly because of avoiding choosing arbitrary programs, although they are most certainly guaranteed to be present. Besides, there are better ways to find the platform architecture, I think. os.uname() comes to mind. ===> os.uname is Unix-only: http://docs.python.org/2/library/os.html#os.uname os.uname() Return a 5-tuple containing information identifying the current operating system. The tuple contains 5 strings: (sysname, nodename, release, version, machine). Some systems truncate the nodename to 8 characters or to the leading component; a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname()). Availability: recent flavors of Unix. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why does platform.architecture default to sys.executable?
On 26/10/13 18:13, Amit Saha wrote: a 64-bit system. Then, you can read /proc/cpuinfo and look for the lm flag. If it is present, it is a 64-bit system, But that will only work on *nix systems I assume? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] why does platform.architecture default to sys.executable?
On Sat, Oct 26, 2013 at 12:39 PM, Albert-Jan Roskam wrote: > > Why does the "executable" parameter default to sys.executable? In general platform.architecture() parses the output of the UNIX "file" command. But on Windows the linkage is hard coded to 'WindowsPE', and bits is the default value calculated from the pointer size in the current process (i.e. python.exe). Use ctypes or PyWin32 instead. ctypes: windll.kernel32.GetBinaryTypeW PyWin32: win32file.GetBinaryType, win32file.SCS_64BIT_BINARY, etc http://msdn.microsoft.com/en-us/library/aa364819 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] taking a tuple with input
p1 = tuple([float(ele) for ele in input('Please type ...').split()]) p2 = tuple([float(ele) for ele in input('Please type ...').split()]) # input format is : 25.0 30.0 - *Siva Cn* *Python Developer* *http://www.cnsiva.com* - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about conditions and empty values
For the program below, if I enter "0" at the prompt, it provides the reply "Please, sit. It may be a while". However, if I just press the Enter key, it shuts the program down without a reply from the Maitre D'. My question is this - the author of this exercise states the condition is False if either zero or "empty" is the value. I'm assuming he means that empty is just pressing Enter without entering a number? He talks about testing for empty values, but I'm not seeing that entering an empty value here yields the response he is talking about. Unless I'm mistaken about something. Thanks in advance for your assistance! #Maitre D' #Demonstrates treating a value as a condition print("Welcome to the Chateau D' Food") print("It seems we are quite full this evening.\n") money = int(input("How many dollars do you slip the Maitre D'?")) if money: print("Ah, I am reminded of a table. Right this way.") else: print("Please, sit. It may be a while.") input("\n\nPress the enter key to exit.") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about conditions and empty values
On 26/10/13 20:13, Shelby Martin wrote: My question is this - the author of this exercise states the condition is False if either zero or "empty" is the value. I'm assuming he means that empty is just pressing Enter without entering a number? Normally that would be correct but... money = int(input("How many dollars do you slip the Maitre D'?")) Here we try to convert the string to an int. and int() fails when given an empty string so your program never reaches the if test. Which begs the question: GHOw are you running your programs? If you used a console or an IDE it should have shown you the error message which would have explained what and where things went wrong. You would need to either use a try/except clause around the conversion or check for an empty string before converting. try/except is the preferred route but you may not have covered that yet. if money: print("Ah, I am reminded of a table. Right this way.") else: print("Please, sit. It may be a while.") If you did get the error message then please, in future, include any such in their entirety in posts because they greatly simplify diagnosing more complex issues. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] taking a tuple with input
On 26/10/2013 20:32, Siva Cn wrote: p1 = tuple([float(ele) for ele in input('Please type ...').split()]) p2 = tuple([float(ele) for ele in input('Please type ...').split()]) # input format is : 25.0 30.0 - *Siva Cn* *Python Developer* *http://www.cnsiva.com* - Congratulations, for managing to break threading you have won tonight's star prize, a two week, all expenses paid holiday to Three Mile Island, Pennsylvania :) -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] taking a tuple with input
On 26/10/13 20:32, Siva Cn wrote: p1 = tuple([float(ele) for ele in input('Please type ...').split()]) If you do it all in one line like that you dpn;t need the [] inside the tuple() call. The generator expressoion will work directly. But of course a single line like that will be hard to debug if you get anything wrong. You have to decide which trade off you prefer, size v complexity. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ 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] why does platform.architecture default to sys.executable?
On Sun, Oct 27, 2013 at 7:46 AM, Alan Gauld wrote: > On 26/10/13 18:13, Amit Saha wrote: > >> a 64-bit system. Then, you can read /proc/cpuinfo and look for the lm >> flag. If it is present, it is a 64-bit system, > > > But that will only work on *nix systems I assume? Indeed, both my answers assumed a Linux system. Sorry about that. -- http://echorand.me ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor