-1

I am trying to make a text-based adventure game. I am new to Python and I am trying to delay the text like in the older games.

from time import sleep
import sys
def de(string):
    for c in string:
        print(c, end='')
        sys.stdout.flush()
        sleep(0.1)

Throughout the code, do things like:

print(de("\nPlayer health: "), player_health,)

or

user_fighter = input(de("what is the name of your fighter?"))

The output always ends in "None"

e.g.
Player health: None 100

How do I fix this?

Ctrl S
  • 1,053
  • 12
  • 31
Benja
  • 3
  • 3
  • What are you exactly trying to do? Can you provide us more info about your program output and some examples? – DecaK Sep 27 '18 at 22:17
  • Please confirm that adding a line after the definition of the de() function in your question e.g. `print de(“Player Health:“)` will print `Player Health:None` ? Please check this then edit this into your question to make your question a Minimal Complete Verifiable Example https://www.stackoverflow.com/help/mcve – DisappointedByUnaccountableMod Sep 27 '18 at 22:19
  • What? How does the code you show produce `**None**` Or, @d_kennetz what are you doing changing this significant aspect of the question? – DisappointedByUnaccountableMod Sep 27 '18 at 22:20
  • 3
    The value your `de()` function returns is `None` which is why you see that value being printed. Just call `de()` directly (i.e. not as an argument in a `print()` call). – martineau Sep 27 '18 at 22:27
  • Does this answer your question? [Random None when printing from raw\_input](https://stackoverflow.com/questions/26922537/random-none-when-printing-from-raw-input) – Georgy Oct 27 '20 at 11:20

2 Answers2

0

Your de function does not return anything, and therefore has a return value of None. Additionally, you already have a print within the function that does what you're trying to do. As a result, you can call the de function directly instead of calling it within a print.

Code

from time import sleep
import sys
def de(string):
    for c in string:
        print(c, end='')
        sys.stdout.flush()
        sleep(0.1)
# ...
# Code
# ...

de("\nPlayer health: ")
print(player_health)

Output

Player health: 100

"Player health" is displayed letter by letter, and the value is displayed at once after.

Ctrl S
  • 1,053
  • 12
  • 31
0

How about having two functions. One that outputs text and another that output text but returns a user's entry.

from time import sleep
import sys

def de(string, end='\n'):
    for c in string:
        print(c, end='')
        sys.stdout.flush()
        sleep(0.1)
    print(end,end='')

def de_input(string):
    de(string, end='')
    return input()

de("Welcome to game")
name = de_input("What is your name?")

de("Welcome %s" % name)
scotty3785
  • 6,763
  • 1
  • 25
  • 35