Home > front end >  Recursion Question in Python. No Conditionals or Loops
Recursion Question in Python. No Conditionals or Loops

Time:02-03

I am trying to figure out how to print the word "hello" 121 times in python. I need to use a function without conditionals or loops, no new lines, and no multiplying the string by an integer.

I thinking something like:

print_hello(): 
   print('hello')
   print_hello()

print_hello()

but I can't seem to find a way to limit the recursive output without conditionals. Any help would be greatly appreciated.

CodePudding user response:

from sys import setrecursionlimit
setrecursionlimit(121)

def phello():
    print('hello', end='')
    phello()
  
try:
    phello()
except:
    print('done')

CodePudding user response:

def print_hello():
    print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello'); print('hello');
  • Related