Input resulting in None

Asked

Viewed 57 times

3

I was trying a new to present the texts in a basic game that I’m creating, which basically gives a sense that the text is being written The code is this below:

def print_slow(txt):
    for x in txt:                     
        print(x, end='', flush=True)
        time.sleep(0.02)
    print()
print_slow("Olá, o dia está belo hoje")

I don’t understand much about functions, but whenever I try to put that function into a input as:

input(print_slow("Olá, como vai?:"))

it works as it should, only that appears a None where you would write:

Olá, como vai?:
None 

someone knows why this occurs, and if there is any way to put a input with this function that leaves the text "slow"??

  • 1

    Remove the print() after the loop and see what happens

  • 2

    @Wilson Bonato I want from the bottom of my heart to thank you for asking this question. Because I had never thought about creating a print slowly and you opened my mind to have several ideas of cool programs to create. <3

2 answers

2


This happens because its function does not have a return and Python automatically returns None when calling the function.

Fixing the problem:

You want to write your code so that the user can type on the same print line ? Example:

Olá, como vai?: Vou bem.

If yes, first you must withdraw the last print() of its function to prevent line breaking. After that, separate the call from the print_slow() and input() on different lines so that the return None of your function is not passed as parameter. Example:

import time

def print_slow(txt):
    for x in txt:                     
        print(x, end='', flush=True)
        time.sleep(0.02)

print_slow("Olá, como vai?")
answer = input(": ")

If for some reason, you do not want to make all these changes to your code, you can do otherwise which I do not recommend much, but can also solve the problem. This form is basically to define an empty string in the return of its function. Example:

def print_slow(txt):
    for x in txt:                     
        print(x, end='', flush=True)
        time.sleep(0.02)
    print()
    return ""

This fixes the problem, because when it returns an empty string, it will be passed as parameter to the input() and the user will not see anything, because the string is empty.

And why don’t I recommend a lot ? Because it doesn’t make sense for its function to return an empty string, even more being a function to print text.

1

this happens by the fact of its function print_slow() returns the value None, simple as that. and the input() also shows a value on the screen, next to the value that print_slow returns which is 'None' since it returns nothing

there are two ways that I imagine in correcting, I will quote below

Option 1

remove the print_slow from inside

def print_slow(txt):
    for x in txt:                     
        print(x, end='', flush=True)
        time.sleep(0.02)

print_slow("Olá, como vai?: ")
x = input()

option 2

change the function

def print_slow(txt, perg: bool = False):
    for x in txt:                     
        print(x, end='', flush=True)
        time.sleep(0.02)
    if perg:
        input()
        print()
    else:
        print()

print_slow("Olá, o dia está belo hoje")
print_slow("Olá, como vai?: ", True)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.