Python Forum
important work. need help urgently please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
important work. need help urgently please
#7
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.
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 = False
So 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
            break
Why 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).
Reply


Messages In This Thread
RE: important work. need help urgently please - by deanhystad - Feb-11-2021, 04:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I need help with a python Rock Paper Scissors game urgently. Riff_Raff 3 8,626 Dec-05-2018, 09:13 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020