0

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

Cai TOBIN
  • 3
  • 1
  • 2
    You are passing 2 parameters - i.e., the constant string 'Did you say' and an input value. The function only expects one parameter – DarkKnight Nov 30 '22 at 07:00
  • What sense does it make anyway? You are waiting for int and convert the input to one. After you pass it to the function it will fail, as it will try to iterate over an int, which "no can do". Also do NOT ever call param str, since it's pythons keyword. And last but not least: hope it's bad tag, cause nobody, literally nobody codes in python 2.x anymore. Unless it's a medieval legacy code... – Gameplay Nov 30 '22 at 07:04
  • @JohnDoe, while you are correct, this i not exactly the case here, CaiTOBIN tried to pass a string and an int – GregoirePelegrin Nov 30 '22 at 07:08
  • @GregoirePelegrin Cobra gave the answer to his question and it's correct one. I'm just saying that if he passes the int into that function the iteration will fail, that's it. – Gameplay Nov 30 '22 at 07:21
  • I don't see how [Cobra's answer](https://stackoverflow.com/questions/74623860/typeerror-print-slow-takes-1-positional-argument-but-2-were-given/74623976#74623976) would be particularly better than [mine](https://stackoverflow.com/questions/74623860/typeerror-print-slow-takes-1-positional-argument-but-2-were-given/74624022#74624022) as long as we don't know if the correct signature is the one in the definition or in the usage. Additionally, I would argue that mine is simpler to understand for beginners. – GregoirePelegrin Nov 30 '22 at 08:02

2 Answers2

0

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, '?')
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • There is no implicit check of the type in my opinion, but a type conversion instead. (The former would imply that the value already is an `int` beforehand, while the latter will try to enforce it by converting the type.) – GregoirePelegrin Nov 30 '22 at 07:13
  • @GregoirePelegrin It's implicit because the conversion is attempted with no explicit check that it succeeds – DarkKnight Nov 30 '22 at 07:45
  • I would say in this case that the type check would be "Is this value `int`-able?" rather than "Is this value an `int`?". Still it eventually amounts to the same thing. – GregoirePelegrin Nov 30 '22 at 07:59
  • 1
    thanks for the answer i will remember this for future thanks so much – Cai TOBIN Nov 30 '22 at 09:59
0

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.

GregoirePelegrin
  • 1,206
  • 2
  • 7
  • 23
  • 1
    thank you for this answer it fixes my issue really simply while allowing me to keep what i use for the defined slow print i will remember to not use the str for my stuff i only used it cuz i have it copy and pasted from another answer thanks – Cai TOBIN Nov 30 '22 at 09:56