0

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
anarchy
  • 3,709
  • 2
  • 16
  • 48
oCosmic
  • 15
  • 1
  • 7
  • 3
    You are printing the result of `print_pattern()`. It returns `None`. That's why you're getting an unexpected output of `None`. – kichik Feb 05 '22 at 18:42
  • can you paste the code instead of a screenshot ? – anarchy Feb 05 '22 at 18:48
  • 2
    *"It is saying I'm not adding a line break at the end."* -- It's not saying that, it's actually saying that your output has two `None`s that shouldn't be there. So I've closed your question as a duplicate of existing questions that cover the same issue/topic. But if there's something I've misunderstood, LMK. – wjandrea Feb 05 '22 at 18:55
  • The code it has immediately is def print_pattern(): print('*****') my solution is the for i in range(2): print(print_pattern()) which gives me the out put of ****** ****** with no white space in between. However, it wants me to add the white space. Is the print statement not defining the print_pattern? – oCosmic Feb 05 '22 at 18:56
  • Also, please read [ask], which covers how to write a good title. And [please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341). Instead, copy the text itself and use the formatting tools like [code formatting](/editing-help#code). – wjandrea Feb 05 '22 at 18:58
  • 1
    wjandrea thanks still learning on how to use this community. Brand new to all of this! – oCosmic Feb 05 '22 at 19:02

1 Answers1

1

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.

anarchy
  • 3,709
  • 2
  • 16
  • 48
  • The problem now is that the name `print_pattern` doesn't match what the function does. I'd actually take this the other direction: print in `print_pattern`, and don't print its return value. – wjandrea Feb 05 '22 at 19:02
  • I cannot change the def print_pattern(): or the print('*****'). – oCosmic Feb 05 '22 at 19:04
  • could you paste the code in your question with the expected output, its hard to see what the output needs to be exactly from a screen shot @wjandrea – anarchy Feb 05 '22 at 19:04
  • oh that's easy then, take a look at my answer in a minute – anarchy Feb 05 '22 at 19:05
  • @anarchy Oh, I'm not OP. – wjandrea Feb 05 '22 at 19:05
  • my bad, @oCosmic take. look at my answer now – anarchy Feb 05 '22 at 19:06
  • def print_pattern(): print('*****') for i in range(2): print(print_pattern()) expected output: ***** ***** my output: ***** none ***** none – oCosmic Feb 05 '22 at 19:07
  • there's white space in between all that. I'm on my phone.-. – oCosmic Feb 05 '22 at 19:08
  • hey @oCosmic I edited you question, could you take a look at it and see if its correct – anarchy Feb 05 '22 at 19:10
  • @oCosmic also please take a look at my answer at the bottom, I think it's the solution you are looking for. – anarchy Feb 05 '22 at 19:11
  • @anarchy thank you so much. From what I understand I was print the function rather than the variable because of the print statement? – oCosmic Feb 05 '22 at 19:11
  • @oCosmic because you already have a print function within the function you are running, you don't have to run the print function the function itself. in a way yes, you are printing the function rather than printing a variable. you should just run the function. if my answer helped please tick it correct. thanks! – anarchy Feb 05 '22 at 19:12