1
str = input("Please enter y to start the program: ")
while str == 'y':
    def printNTimes(s, n):
        for i in range(n):
            print(s)

    phrase = input("Enter a string: ")
    number = int(input("Enter a positive number: "))

    allTogether = printNTimes(phrase, number)

    print(allTogether)
    print()    
    str = input("Please enter y if you want to continue: ")
print("DONE")

Output:

Please enter y to start the program: y
Enter a string: Hi
Enter a positive number: 3
Hi
Hi
Hi
None

Please enter y if you want to continue:
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Junkhead
  • 9
  • 4
  • 1
    `printNTimes` already does the printing and doesn't return anything, which means it will return `None`. You assign that result to `allTogether` and then proceed to print `allTogether`, which prints `None`. Just call `printNTimes` and don't assign the result to a variable and don't print it. – Grismar Nov 19 '21 at 03:20
  • @Grismar, `printNTimes` is inside the while loop. – PCM Nov 19 '21 at 03:20
  • It is now, but it wasn't when I wrote the comment. – Grismar Nov 19 '21 at 03:20
  • Why the function printNTimes when you can do simple like this -> print(phrase*number) – Ruthvik Nov 19 '21 at 03:24
  • Another thing is the function printNTimes doesn't return any value but you are trying to print a value from it. So None is returned – Ruthvik Nov 19 '21 at 03:25
  • @Ruthvik This is for an assignment. I have to use a function that accepts user inputs for the string and the integer. – Junkhead Nov 19 '21 at 03:28
  • @Brain Correa, So the simplest solution is to remove this line -> print(allTogether) – Ruthvik Nov 19 '21 at 03:35
  • Beside the point, but `str` is a bad variable name since it [shadows](https://en.wikipedia.org/wiki/Variable_shadowing) the [builtin `str` type](https://docs.python.org/3/library/stdtypes.html#str). Use a more descriptive name, or at least something like `s`. For an example problem this could cause, see [TypeError: 'list' object is not callable in python](/q/31087111/4518341). – wjandrea Nov 19 '21 at 03:37
  • @wjandrea Ah ok, I'll fix the variable name as well. I got my program to work now. I appreciate the input from everyone! This is my first semester learning Python so I'm trying to better my programming etiquette. – Junkhead Nov 19 '21 at 03:43

3 Answers3

2

You don't need print(allTogether), or the variable itself, because when you print it, you get an extra None (because the function returns None i.e, it does not return anything).

Also, put the function outside the while loop so that it is easier to read.

def printNTimes(s, n):
    for i in range(n):
        print(s)

str = input("Please enter y to start the program: ")
while str == 'y':
    phrase = input("Enter a string: ")
    number = int(input("Enter a positive number: "))

    printNTimes(phrase, number)
    print()    
    str = input("Please enter y if you want to continue: ")
print("DONE")

Just call the function. You could use return and then print the function, but this might be easier.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
PCM
  • 2,881
  • 2
  • 8
  • 30
  • Okay, this helped a lot. Thank you for the feedback plus the tip on putting the function outside the while loop. I'm in an introduction to Python class, so I'm still learning proper programming etiquette. – Junkhead Nov 19 '21 at 03:36
0

The problem is the function printNtime is actually being used as a subroutine, meaning it doesn't RETURN any values.

I am not sure what you want as a final output so here's two solution.
IF USED AS SUBROUTINE: just remove the print(allTogether)
IF USED AS A FUNCTION: you need to create a string variable in the function and return it.

def printNTimes(s, n):
  mystring = ""
  for i in range(n):
    mystring = mystring + s + "\n"
  return mystring
Kurt Rojas
  • 319
  • 2
  • 12
  • Presumably, a function called "printNTimes" is going to do the printing for you, so the subroutine option makes more sense. – wjandrea Nov 19 '21 at 03:31
0

The issue you're seeing is because your routine, printNTimes(), returns a None value. You say:

allTogether = printNTimes(phrase, number)

print(allTogether)

allTogether is set to None because printNTimes does not return a value. When you call print(allTogether), that's what is printing the None.

It sounds like you intended your printNTimes() routine to assemble one big string of all the output and return it, which you would then print out via the print(allTogether) call. But your printNTimes() routine is calling the print() method and outputing the text then. So, you need to rewrite your printNTimes() method. Also, note that defining that function inside the loop is not necessary. Once is sufficient. So something like this should suffice:

def printNTimes(s, n):
    s_out = ""
    for i in range(n):
        s_out += s + '\n'
    return s_out
str_ = input("Please enter y to start the program: ")
while str_ == 'y':

    phrase = input("Enter a string: ")
    number = int(input("Enter a positive number: "))

    allTogether = printNTimes(phrase, number)

    print(allTogether)
    print()    
    str_ = input("Please enter y if you want to continue: ")
print("DONE")

Also note that I renamed your str variable to str_. str is a Python type, a reserved word that should not be reused.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32