This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
I have been trying to get this to output correctly. It is saying I'm not adding a line break at the end.
I was wondering, how I could add the line break? From my understanding the code is for the most part right.
I also need to have it take in another output that Zybooks generates itself, so I can't just simply put two print statements of ('*****')
def print_pattern():
print('*****')
for i in range(2):
print(print_pattern())
Expected output:
*****
*****
My output:
*****
None
*****
None
If you want your function to work, you have to return a value like this.
def print_pattern():
return '*****'
for i in range(2):
print(print_pattern())
You function isn't working properly because you are trying to print something that has no return value. If you return something and try to print like I have done in my code here, it will work.
Edit.
Since you cannot change the print_pattern() function, the correct way to do this would be like this.
def print_pattern():
print('*****')
for i in range(2):
print_pattern()
You just do a for loop where you run the function at the end of each loop. The print function my default adds a new line at the end of the print.
Related
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 1 year ago.
I would like to restart my python code from the beginning after a given "yes" input from the user, but I can't understand what I'm doing wrong here:
if input("Other questions? ") == 'yes' or 'yeah':
main()
else:
pass
my code is included within the function main().
Thanks for the help!!
You would probably do it with a while loop around the whole thing you want repeated, and as Loic RW said in the comments, just break out of the while loop:
while True:
# do whatever you want
# at the end, you ask your question:
if input("Other questions? ") in ['yes','yeah']:
# this will loop around again
pass
else:
# this will break out of the while loop
break
This question already has answers here:
Cannot call a variable from another function
(4 answers)
Closed 2 years ago.
I have got a database program to keep data in, and I can't solve this problem:
I have got two functions. When you input A into the program
the function called addy() starts
and ask for more input into a variable
then it returns to the main screen,
then the user can Input S
which starts Show()
and then it's supposed to show what you have added into the variable
PROBLEM:
It's not getting the value from the previous definition.
CODE:
def addy():
os.system('cls')
addel = input('what is the name of the operating system?: \n')
os.system('cls')
time.sleep(1)
print(addel + ' Has been added to the database!')
time.sleep(2)
program()
def show():
print('Heres a list of the operating systems you have added:')
time.sleep(5)
program()
addel = addy()
print(addel) # this should print the value from the previous function
The are 2 reasons why
Addel is a local variable not a global one. Therefore, you can only use it in your addy function.
Say your intent was not to use it which is what it seems, you wrote
addel = addy()
the function addy has no return value so your code wont work.
to fix this write
return addel
as the last line in your addy function then it will work because now the function has a return value.
This question already has answers here:
How to print without a newline or space
(26 answers)
How can I flush the output of the print function?
(13 answers)
Closed 2 years ago.
im a beginner at python and i've come across what is probably a simple problem.
I want the code below to print the "." x times, each .100 of a second after each other. This is what ive got, but it just prints it all at once after x * .100 seconds. It would also help if you could redirect me to something that explains why it dosnt work or if you explained why it dosnt work.
import time
for i in range(x):
print(".", end="")
time.sleep(.100)
Thanks in advance.
PS. If the code is completely wrong please say so.
Just printing doesn't mean that the content is flushed - i.e. it can still be in a buffer in your terminal or execution environment.
You can append flush=True to the arguments to print in python3 to make it flush the output as well:
import time
for i in range(x):
print(".", end="", flush=True)
time.sleep(.100)
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 3 years ago.
I am making a command line game engine in python. However, when I attempt to print a command, it newlines and creates a jittery frame.
Adding the end attribute bogs down my computer and nearly crashes the shell. The dupe uses sys.stdout.write('') newline sys.stdout.flush or print'', or print('',end=''). all bog down shell and crash it. print('') doesn't bog down though which is weird.
#this is enough code to demonstrate the problem
while true:
print(' = === ===== ======= ========= =========== ============= YYYYYYYYYYY ================================================================================')
#crash issue
import sys
while True:
sys.stdout.write('mooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo')
sys.stdout.flush()
I expect the screen to fill, instead it wobbles up and down.
I am not sure if I understood your question correctly, but I’m thinking you do not want to print a new line with each call to the print() function.
If so, the print function has an optional argument end, that is set by default as \n (this, creating a new line if not specified otherwise). If you don’t want this to happen, you can simply use the print function as:
print(your_argument, end=“”)
Replacing your_argument with whatever you want to print out.
This question already has answers here:
What is the best way to toggle python prints?
(6 answers)
Closed 7 years ago.
I have 10 or 15 very useful debugging print statements sprinkled throughout my program (in different functions and in main).
I won't always want or need the log file though. I have a config file in which I could add a parameter to toggle print statements on or off. But then, I'd have to add a guard check for the value of this parameter above every print statement.
What are some better approaches?
from __future__ import print_function
enable_print = 0
def print(*args, **kwargs):
if enable_print:
return __builtins__.print(*args, **kwargs)
print('foo') # doesn't get printed
enable_print = 1
print('bar') # gets printed
sadly you can't keep the py2 print syntax print 'foo'