[Tutor] New Line Problem
I am having a problem with creating a new line in a return statement for a function. It has a mix of a bunch of strings and variables. Everything that I want included will print, except for the new lines. I have tried "\n" but with no luck. Do you have any recommendations? Thanks. http://py3.codeskulptor.org/#user301_SIdBE94Q8yi3qtM.py (it's the second function) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New Line Problem
Anna Lapre wrote: > I am having a problem with creating a new line in a return statement for a > function. It has a mix of a bunch of strings and variables. Everything > that I want included will print, except for the new lines. I have tried > "\n" but with no luck. Do you have any recommendations? Thanks. > > http://py3.codeskulptor.org/#user301_SIdBE94Q8yi3qtM.py > (it's the second function) "\n" is indeed what you need: >>> equal = "" >>> index = 2 >>> line1 = "helllo" >>> line2 = "heillo" >>> print(line1 + "\n" + equal[:index] + "^\n" + line2) helllo ==^ heillo Note that you can repeat a string with >>> "=" * 3 '===' >>> "ab" * 2 'abab' which combined with str.format allows the following alternative solutions: >>> print("{}\n{}^\n{}".format(line1, "=" * index, line2)) helllo ==^ heillo # requires Python 3.6 >>> print(f"{line1}\n{'='*index}^\n{line2}") helllo ==^ heillo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New Line Problem
On 02/02/18 02:44, Anna Lapre wrote: > I am having a problem with creating a new line in a return statement for a > function. It has a mix of a bunch of strings and variables. Everything that > I want included will print, except for the new lines. I have tried "\n" but > with no luck. Do you have any recommendations? Thanks. > > http://py3.codeskulptor.org/#user301_SIdBE94Q8yi3qtM.py > (it's the second function) For a short function like this just post the code in the message, its much easier to work with. def singleline_diff_format(line1, line2, idx): length_line1 = len(line1) length_line2 = len(line2) if (idx + 1) <= length_line1 and (idx + 1) <= length_line2: equal = "" code = (line1 + equal[0:idx] + "^" + line2) return code else: return "" You just need to add newline characters around the equal line. code = (line1 + '\n'+ equal[0:idx] + "^" + '\n' + line2) Or using a format string: code = "%s\n%s\n%s" % (line1,equal[:idx]+'^',line2) You also are not implementing the part of your specification that says return an empty string if either input contains a newline. testing the lengths does not work: >>> s = "fred\njones" >>> len(s) 10 You need to test whether '\n' is in the string. By the way, your first function is much more complicated than it needs to be, it is very inefficient and I'm pretty sure its wrong too. Try: singleline_diff("part trap", "part part") And see if you get the right answer. But you can make it a whole lot simpler if you just compare the characters one by one and stop messing around with indexes. -- 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] Updates to web tutor
On 15/11/17 17:41, Alan Gauld via Tutor wrote: Hopefully my last on this topic. I've just uploaded the new landing page for my web site. Aside from being more mobile friendly it also has a wider remit than before, being a gateway to more than just my programming tutorial. As ever feedback is appreciated (link is in the .sig). -- Alan G http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python36.dll
You should not be trying to copy Python.exe by hand, you should use the installer: https://www.python.org/downloads/release/python-364/ Use the installer and let it install Python to where it needs to go. If you still have problems, you will have to tell us how you are trying to start Python and the EXACT error message you get. On Thu, Feb 01, 2018 at 06:43:11PM +0800, House WANG wrote: > > Dear python manager: > > I can use python3.6.4,I can use 3.6.4 zip, but I cannt use python 3.6.4exe > > version, when installed 3.6.4exe, open cmd python, pop python36.dll > > missing. I copy 3.6.4zip(unzip) python36.dll into system32 and systemWow64 > > both, it cannt solve the bug! > > Iuse win10 64 > > Hope your answer! -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor