0
import time, random, sys

def print(s):
  for c in s + '\n':
    sys.stdout.write(c)
    sys.stdout.flush()
    time.sleep(1./20)

print("\nHello,\n")
print("You can choose out of 5 tasks : ")
print("task 1 | add 3 numbers")
print("task 2 | multiple numbers")
print("task 3 | enter a number between 10 and 20")
print("task 4 | count until 200")
print("task 5 | guess the number")

x = int(input(print("\nWhich tasks do you want to preform? | ")))

when i use this code, it wil type "none" after its done typing x. can anyone explain to me why that happens? and how i can fix it. or how i can slow type a input. (its part of a bigger code, but this are the first couple lines of code)

  • Remove the print statement inside of the input: `x = int(input("\nWhich tasks do you want to perform? | "))` – gmdev Sep 28 '20 at 16:27
  • Better duplicate target (see also linked questions): [Random None when printing from raw_input](https://stackoverflow.com/q/26922537/7851470) – Georgy Oct 27 '20 at 11:26

1 Answers1

0

when i use this code, it wil type "none" after its done typing x. can anyone explain to me why that happens?

This is because you use input() incorrectly. From the Python documentation:

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

Since your custom print() function has no return statement, it returns None by default and that value is passed to input() as the "prompt" which input() then prints out.

Also, Python already has a function named print(). Using the same name as a built-in Python function will lead to pain and suffering, so don't do it. In fact, you don't need to write a print() function at all, since this functionality is already built in.

In otherwords, you can get the same behavior with:

input('Which tasks do you want to preform? | ')

Alternatively, you can call input() without any parameter:

x = int(input())
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks for the reaction. If i remove the print, which im gonna give a different def, then the input will not slowprint anymore, which is what i wanted. Is there a way to slowprint a input? – Matheun Struik Sep 28 '20 at 21:56
  • @MatheunStruik I think your two options are to call `input()` without a parameter or to write your own version of `input()` that reads from stdin. – Code-Apprentice Sep 28 '20 at 22:12
  • 1
    @MatheunStruik use your custom print function before getting the input. Like this: `print("..."); x=int(input()) ` – Asocia Sep 28 '20 at 22:35
  • @Code-Apprentice do you maybe know a custom input i can use, i cant find any – Matheun Struik Sep 29 '20 at 08:01
  • @MatheunStruik By "custom", I mean write it yourself similar to the way you wrote your own `print()`. I think for your purposes, using the standard `input()` with no parameters works just fine. – Code-Apprentice Sep 29 '20 at 15:30