0
call a function with an argument
Hi guys, I'm new to python and I need your help. I want to call a function which prints a range(x, y) defined by an argument. For example this simple function: def func(): for x in range(0, 100): print("hello") func() How to define the range by the argument instead of the func itself? Thank you and sorry for bad English!
2 odpowiedzi
+ 3
def printHello(start=0,stop=100):
for i in range(start,stop):
print("Hello")
# putting start=0 and stop=100 if you don't pass the two arguments: 0 and 100 will be the 2 values ...
>>> printHello()
Hello
.... (99 times)
# if you pass the two arguments (different from 0 and 100) the new arguments will be used by the function
>>> printHello(1,6)
Hello
Hello
Hello
Hello
Hello
+ 1
def func(s,f):
for x in range(s, f):
print("hello")
func(1,5)