-1

I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.

def printFunction(n):
    i = 1
    while i <= n:
        print(i)
        i+=1


print (printFunction(int(input())))
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Benjamin Safari
  • 89
  • 2
  • 11

1 Answers1

1

You can use this code to prevent none, tough its just the last line changed

def printFunction(n):
i = 1
while i <= n:
    print(i)
    i+=1
printFunction(int(input()))

In the last line you were using print(printFunction(int(input()))) which was getting you None after printing the results.

Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34