-1

Hello i need help understanding what all of the lines does in this code

I know what the code itself does, it writes one letter at a time but i dont understand what all the lines means and such like "%s" thanks

import time
import sys
def delay_print(s):
    for c in s:
        sys.stdout.write( '%s' % c )
        sys.stdout.flush()
        time.sleep(0.01)
4

1 回答 1

1

You should read this: Pygame Tutorial

import time, sys

this load modules. sys is for the stdout function.
time is for the sleep function.

def delay_print(s):

def is the keyword for a new function.
delay_print is the name of the function.
s is an argument.


Here is a really easy to understand example:

def coffee_mashine( coffee_beans ):
    # make coffee
    return coffee

for c in s:

for is the keyword for a loop.
c is a character for example: a, b, c.
s is a string for example: "Hello World"

sys.stdout.write( '%s' % c )

sys.stdout.write() is a function-call.
the first argument is a String. for example: "My char is %s"
%s is a wildcard for a variable.
%c is a variable

sys.stdout.flush()

clear the stdout.

time.sleep(0.01)

wait a short time.


Here a much easier code to do that:

import time, sys

def delay_print(s):
    for c in s:
        print c
于 2013-10-22T09:17:26.567 回答