Ask coding questions

← Back to all posts
Is there a way to go back to the beginning of a function using an if gate?
TristanMisja (8)

NOTE: This is only a sample of the code, and it won't show tabs.

import sys
import os
import string
import math
import colorama
import time

def login():
global username
global password
uy = None
cp
username = input("What is your username?\n\t")
uy = input("Username is " + username + ". Is this true? (y/n)")
if uy == "y":
continue
elif uy == "n":
#This is where I want it
password = getpass.getpass("What is your password?\n\t")
cp = getpass.getpass("Confirm your password:\n\t")
if cp == password:
continue
else:
#This is also where I want it

Commentshotnewtop
heyitsmarcus (287)

What you really want to do is restructure your logic just a bit especially in the first part where you want to start over. Instead of saying:

if uy == "y":
 continue
elif uy == "n":

Do something like:

if uy == "n":
  login()
elif uy == "y":
  #continue on with the rest of the password code

And then in the bottom of the script you could just call login() again to restart the process.

If this helped solve your problem, please consider upvoting and marking my answer as the accepted answer to close this question. I will continue to answer any questions you might have.

TristanMisja (8)

@heyitsmarcus Ok I'll test this I'm just not sure if you can use a function inside itself... but thx!

heyitsmarcus (287)

@TristanMisja You absolutely can do that. Here is a repl with the code I talked about: https://repl.it/@heyitsmarcus/Python-Username-Password