But i want it to print the output all in one line. I want to be able to decide on whether i allow it to print the items on a new line, or to print them all on one line.
I have no idea what you are trying to do but your function is not called.
I guess you are trying to put that function in the interpreter namespace for other uses.
The function has a useless line and looks as though you want Python version 2.x.x and the print STATEMENT.
If you require version 3.x.x then you need the print FUNCTION and the end=" " _flag_.
#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]
def printWithoutNewlines():
for objects in mylist:
objects = objects.replace('hello', "hi")
# Note the comma below.
print objects ,
printWithoutNewlines()
Last login: Sun May 27 10:36:33 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> chmod 755 loop.py
AMIGA:barrywalker~/Desktop/Code/Python> ./loop.py
hi you are so cool
AMIGA:barrywalker~/Desktop/Code/Python> _
thank you. i intended to put the following in my original code:
printWithoutNewlines()
but it looks like its not part of what i copied and pasted.
another problem im having is, before and after implementing your suggested change, i have been unable to put the output of the called function into a variable.
what I'm intending to do with the above code is to run the defined function, then, remove the new lines from the output of the function.
When I run the above code, I get this error:
hi
you
are
so
cool
Traceback (most recent call last):
File "./vego.py", line 10, in <module>
functionsOutput.replace('\n', ' ')
AttributeError: 'NoneType' object has no attribute 'replace'
Again I have no idea why so convoluted a method as your function:
DEMO:
#!/usr/bin/python2.7
mylist = [ "hello", "you", "are", "so", "cool" ]
STRING = ' '.join(mylist)
NEWSTRING = STRING.replace('hello', "hi")
print STRING
print NEWSTRING
print
# This function should work on ANY version of Python from at least 1.4.
def printWithoutNewlines():
mystring = ""
for objects in mylist:
objects = objects.replace('hello', "hi")
mystring = mystring + objects + " "
return(mystring)
print printWithoutNewlines()
functionsOutput_sp = printWithoutNewlines()
print functionsOutput_sp
functionsOutput_nl = functionsOutput_sp.replace(' ', '\n')
print functionsOutput_nl ,
Results, same machine as before.
Last login: Mon May 28 14:14:06 on ttys000
AMIGA:barrywalker~> cd ~/Desktop/Code/Python
AMIGA:barrywalker~/Desktop/Code/Python> python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> execfile("loop.py")
hello you are so cool
hi you are so cool
hi you are so cool
hi you are so cool
hi
you
are
so
cool
>>> dir()
['NEWSTRING', 'STRING', '__builtins__', '__doc__', '__name__', '__package__', 'functionsOutput_nl', 'functionsOutput_sp', 'mylist', 'printWithoutNewlines']
>>> exit()
AMIGA:barrywalker~/Desktop/Code/Python> _