Feb-11-2021, 04:28 PM
(This post was last modified: Feb-11-2021, 08:49 PM by deanhystad.)
You don't seem to understand how Python works when loading a module. Code that does not appear inside a function is executed when you import a module or run a python program. All of this code runs immediately because it is not inside a function.
Slightly further down in the file you do this:
In your program there should be almost no code that is outside a function. You should have the import statements at the top of the file. You may have a couple of global variables defined at the top of the file, though global variables are a bad idea. This should be followed by a bunch of functions, and then at the very bottom you have one command that calls choices(). Maybe 5 lines of code that are outside of a function (def).
import random import time import re i = 0 Player1Points = 0 Player2Points = 0 Player1Tiebreaker = 0 Player2Tiebreaker = 0 Winner_Points = 0 Users = open ("Usernames.txt.", "r+") Passwords = open ("Passwords.txt.", "r+") User_array = Users.read().splitlines() Password_array = Passwords.read().splitlines() User1 = False User2 = FalseSo you immediately set Player1Points = 0 and immediately open the file "Usernames.txt". So far so good.
Slightly further down in the file you do this:
Users = open ("Usernames.txt.", "r+") #reopens the files Passwords = open ("Passwords.txt.", "r+") def registration_p2(): registration_p2 = True# this function does the exact same except this time its for player 2 while registration_p2 == True: user_2= input("Enter username for p2") password_2 = input("Enter password for p2") if user_2 in User_array: print("That account is already taken, sorry :(") else: Users.write(user_2) Users.write("\n") Passwords.write(password_2) Passwords.write("\n") Users.close() Passwords.close() print("Great now you can log in :)") registration_2 = False breakWhy are you opening "Usernames.txt" again? Do you think this code happens after registration_p1 but before retistration_p2? It doesn't. registration_p1 and registration_p2 aren't executed until called by choices(). So your code is doing this:
Users = open ("Usernames.txt.", "r+") Passwords = open ("Passwords.txt.", "r+") User_array = Users.read().splitlines() Password_array = Passwords.read().splitlines() User1 = False User2 = False Users = open ("Usernames.txt.", "r+") #reopens the files Passwords = open ("Passwords.txt.", "r+")You do this throughout your program, interspersing code that will execute as soon as the program is run with code that is only executed when the function is called.
In your program there should be almost no code that is outside a function. You should have the import statements at the top of the file. You may have a couple of global variables defined at the top of the file, though global variables are a bad idea. This should be followed by a bunch of functions, and then at the very bottom you have one command that calls choices(). Maybe 5 lines of code that are outside of a function (def).