Jump to content

Python Functions Forward Definitions

So i am trying to make it possible so in one function it will define a variable, but above that i need another function to use that same variable (this is because i am calling that fiunction in the first function). How can I do this?

import re

def rollDieWithPoints():
   print("hi")
   print(user1)
   print(user2)
   print("hi again")

def authentication():
    file = open("users.txt")
    text = file.read()
    user1NameInput = input("What is your name user 1?: ")
    re_res = re.search(r'^{}, (.*)$'.format(user1NameInput), text, re.MULTILINE)
    if re_res:
        password = re_res.group(1)
        user1PasswordInput = input("What is you password user 1?: ")
        if password == user1PasswordInput:
           print("Welcome to the game user 1, now for user 2")
           user2NameInput = input("What is your name user 2?: ")
           re_res = re.search(r'^{}, (.*)$'.format(user2NameInput), text, re.MULTILINE)
           if re_res:
              password = re_res.group(1)
              userPasswordInput = input("What is you password user 2?: ")
              if password == userPasswordInput:
                 print("Welcome to the game, now you can both play")
                 user1 = user1NameInput
                 user2 = user2NameInput
                 rollDieWithPoints()
              else:
                 print("Wrong password, try again")
           else:
              newUsernameInput = input("That username is not in the system, would you like to create a new user by that name? (Y/N): ")
              if newUsernameInput == "Y":
                 file = open("users.txt", "a")
                 newUsernamePasswordInput = input("Please enter your password?: ")
                 file.write("\n" + userNameInput + ", " + newUsernamePasswordInput)
                 file.close()
              elif newUsernameInput == "N":
                 print("Okay, username incorrect, try again")
              else:
                 print("That is not an input, please try again")
        else:
            print("Wrong password, try again")
    else:
       newUsernameInput = input("That username is not in the system, would you like to create a new user by that name? (Y/N): ")
       if newUsernameInput == "Y":
          file = open("users.txt", "a")
          newUsernamePasswordInput = input("Please enter your password?: ")
          file.write("\n" + userNameInput + ", " + newUsernamePasswordInput)
          file.close()
       elif newUsernameInput == "N":
          print("Okay, username incorrect, try again")
       else:
          print("That is not an input, please try again")
    file.close()

def start():
    authentication()

start()

I want the initial definitions to happen in authentication() and then the use to happen in rollDieWithPoints().

 

Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

# Define a function that takes two arguments
# user1 and user2.
def rollDieWithPoints(user1, user2):
  print(user1)
  print(user2)
  
# In your authentication method, you can simple call
# the rollDieWithPoints function, passing your two
# variables as the arguments.
def authentication():
  # ...
  if password == userPasswordInput:
    rollDieWithPoints(user1NameInput, user2NameInput)
  # ...

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, mshaugh said:

# Define a function that takes two arguments
# user1 and user2.
def rollDieWithPoints(user1, user2):
  print(user1)
  print(user2)
  
# In your authentication method, you can simple call
# the rollDieWithPoints function, passing your two
# variables as the arguments.
def authentication():
  # ...
  if password == userPasswordInput:
    rollDieWithPoints(user1NameInput, user2NameInput)
  # ...

 

Thanks, i knew it was a simple solution but not exactly what the solution was!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×