0

I have this code:

def termi():
    username=input("input username= ")
    to_deny = "!? /,.[]()"
    if any(char in username for char in to_deny):
        print("denied characters: ?, !, space(s), /, dots, [], ()")
        quit()
    elif len(username) <= 2:
        print("username must be at least 3 characters")
        quit()
    else:
        print(f"hello {username}!")
        cmd=input("command: ")
    if cmd=='print':
        printer=input("input print:")
        print(printer)
    elif cmd=='help':
        print("commands: print, help, close/quit, sysver, date+time")
    elif cmd=='close':
        print("closing")
        quit()
    elif cmd=='quit':
        print("quitting")
        quit()
    elif cmd=='help print':
        print("usage: print (args)")
    elif cmd=='help close':
        print("usage: close")
    elif cmd=='help quit':
        print("usage: quit")
    elif cmd=='sysver':
        print("system version: 0.4 beta")
        print("latest update: 1:18 am (pst) on june 18th")
    elif cmd=='date+time':
        import datetime
        time=datetime.datetime.now()
        print(f"the date+time is: {time}")
    else:
        print("invalid!")
def again():
    terminal = input("run terminal again?")

    if terminal == 'Y':
        termi()
    elif terminal == 'N':
        quit()
    else:
        again()

I tried to use functions to make the code loop. But when I run this code, no output appears. What is wrong, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • You have defined two functions. Did you call either of them? – Tim Roberts Jun 24 '22 at 05:18
  • The again function is meant to be called when someone puts a invalid answer, and the termi function is meant to be called when someone puts Y as an answer, so yes, both were called – SomeAndroidCoder Jun 24 '22 at 05:21
  • The posted code does not show that any of the functions are called. Is there some other part of your code that calls `again()` ? – Gino Mempin Jun 24 '22 at 05:22
  • 1
    Your title says "*trying to make a loop using def keyword*". The `def` is not used to make a loop, it is used to `def`ine a function: https://docs.python.org/3/tutorial/controlflow.html#defining-functions. You define a function then you need to call it. – Gino Mempin Jun 24 '22 at 05:23
  • Welcome to Stack Overflow. Please read [ask] and [mre], and note well that this is **not a discussion forum**. Please carefully study how I [edit]ed the post in order to ask the question directly and remove irrelevant explanation. – Karl Knechtel Jun 24 '22 at 06:06

1 Answers1

0

Do NOT use recursion like this. That's not what it was designed for. Instead:

def again():
    while True:
        terminal = input("run terminal again?")

        if terminal == 'Y':
            termi()
        elif terminal == 'N':
            return

again()
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30