Home > Net >  typeerror print_slow() takes 1 positional argument but 2 were given
typeerror print_slow() takes 1 positional argument but 2 were given

Time:11-30

so im fiddling around in pytbon cuz im bored and i realise i try to slow print a input i have earlier in the code i bave defined slow print ive imported every thing i need but when i run it it saw its got 1 positional argument buts been give 2 and im not that good at coding and am only a young student so coupd anyone be a huge help and explain it in basic terms

`

import sys
import os
import time

def print_slow(str):
    for letter in str:
        sys.stdout.write(letter)
        sys.stdout.flush()
        time.sleep(0.1)

num1 = int(input("Chose any number: "))

print_slow("Did you say",num1)

so my issue is that i cant seem to get it to slow print i expected this to work like it always does but i've never slow printed an input before

CodePudding user response:

You are implicitly checking that the input value is of type int by converting the input string. If you insist on doing that then you might find this easier:

from time import sleep

def print_slow(*args):
    for arg in args:
        for c in str(arg):
            print(c, flush=True, end='')
            sleep(0.1)
    print()

v = int(input('Choose any number: '))

print_slow('Did you say ', v, '?')

CodePudding user response:

Replace print_slow("Did you say", num1) by print_slow(f"Did you say {num1}") and you should be good to go. This is (in my opinion) a slightly simpler solution than Cobra's. Here we are using Formatted strings(example) which you will encounter often going forward and that you should take a couple of minutes to understand.
Also, as mentioned in the comments of your post, DO NOT NAME A VARIABLE str (or more generally, do not name a variable with a type name) for it will mess things up later.

  • Related