Feb-11-2021, 01:11 AM
can someone help with this please it keeps saying player_1 isn't defined?
what I need to do for this is authenticate a user after they create an account then allow them to see scores IF they want and/or let them play the game. the bit for the winners isn't working and its due tomorrow and my teacher isn't answering his email (please no one copy this if you are doing the same work our stuff has been submitted to the board its only being re submitted so you will fail :(
how do I extract the players name from the file to store the winner? idk if that makes sense
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
def registration_p1(): # using a function to allow player 1 and 2 to create an account
registration_p1 = True
while registration_p1 == True:
user_1 = input("Enter username for p1")#asks user to create username
password_1 = input("Enter password for p1")#asks user to create password
if user_1 in User_array: # if the details are already found in the text files, the program will inform them
print("That account is already taken, sorry :(")
else:
Users.write(user_1) #saving username to usernames text file
Users.write("\n")
Passwords.write(password_1)#saving password to passwords text file
Passwords.write("\n")
print("Thanks")
registration_1 = False
break
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
def LoggingIn_1(): # function to authenticate player 1
user_1guess = input("Player One, please enter your username: ")
password_1guess = input("Player One, please enter your password: ")
while (user_1guess not in User_array) or (password_1guess not in Password_array):
print("Wrong, closing program ") #if the array cannot find the users supposed log in details it informs them
quit()
user_1guess = input("Player One, please enter your username: ")
password_1guess = input("Player One, please enter your password: ")
else:
player_1 = user_1guess
print ("Hi Player One, I hope you enjoy the game!") #if it is found, the player can move on
User_1 = True
def LoggingIn_2(): #this function does the same for player 2
user_2guess = input("Player Two, please enter your username: ")
password_2guess = input("Player Two, please enter your password: ")
while (user_2guess not in User_array) or (password_2guess not in Password_array):
print("Wrong, closing program ")
quit()
user_2guess = input("Player Two, please enter your username: ")
password_2guess = input("Player Two, please enter your password: ")
else:
player_2 = user_2guess
print ("Hi Player Two, I hope you enjoy the game too!")
User_2 = True
def roll():
Player1Points = 0
Player2Points = 0
points = 0
die1 = random.randint(1,6)
die2 = random.randint(1,6)
dietotal = die1 + die2
points = points + dietotal
if dietotal % 2 == 0:
points = points + 10
else:
points = points - 5
if die1 == die2:
die3 = random.randint(1,6)
points = points +die3
return(points)
for i in range(1,5):
Player1Points += roll()
print('After this round ',player_1, 'you now have: ',Player1Points,' Points')
time.sleep(1)
Player2Points += roll()
print('After this round ',player_2, 'you now have: ',Player2Points,' Points')
time.sleep(1)
if Player1Points == Player2Points:
while Player1Tiebreaker == Player2Tiebreaker:
Player1Tiebreaker = random.randint(1,6)
Player2Tiebreaker = random.randint(1,6)
if Player1Tiebreaker > Player2Tiebreaker:
Player2Points = 0
elif Player2Tiebreaker > Player1Tiebreaker:
Player1Points = 0
if Player1Points>Player2Points:
Winner_Points = Player1Points
winner_User = player_1
winner = (Winner_Points, player_1)
elif Player2Points>Player1Points:
Winner_Points = Player2Points
winner = (Winner_Points, player_2)
winner_User = player_2
print('\nWell done, ', winner_User,' you won with ',Winner_Points,' Point\n\n')
leaderboard1 = "{}:{}".format(Winner_Points,winner_User)
with open ("Leaderboard.txt", "a") as file:
file.write("\n")
file.write(leaderboard1)
f = open('Leaderboard.txt', 'r')
leaderboard = [line.replace('\n','') for line in f.readlines()]
f.close()
leaderboard.sort(reverse=True)
with open('Leaderboard.txt', 'w') as f:
for item in leaderboard:
f.write("%s\n" % item)
print("TOP 5 SCORES:")
with open("Leaderboard.txt", "r") as f:
counter = 0
N = 1
for line in f:
head,sep,tail = line.partition(":")
print("Place No.{}\nUsername: {}Score: {}\n".format(N,tail,head))
N = N + 1
counter += 1
if counter == 5: break
def choices(): # using a function so it is quicker and more efficient, its to output the options to the users and allow them to choose what to do
choices_users = input("Enter 1: to make an account, 2: to log in or 3: to display scores or 4:to play(after you login)")
if choices_users != "1" or "2" or "3" or "4":
print ( "pick a valid option please")
if choices_users == "1": # if they choose "1", the functions for logging in shown previousky are called
registration_p1()
registration_p2()
choices()#then it goes back to the option page
if choices_users == "2":
LoggingIn_1()#the function to sign up is called when "2" is choosen
LoggingIn_2()
choices()#then it goes back to the option page
if choices_users == "3": #this opens the leaderboard and outputs the highest player
scoreboard = open ("scores.txt", "r")
print ("\n")
print("highest player is: ")
scores.close
else:
roll()
choices()
what I need to do for this is authenticate a user after they create an account then allow them to see scores IF they want and/or let them play the game. the bit for the winners isn't working and its due tomorrow and my teacher isn't answering his email (please no one copy this if you are doing the same work our stuff has been submitted to the board its only being re submitted so you will fail :(
how do I extract the players name from the file to store the winner? idk if that makes sense
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
def registration_p1(): # using a function to allow player 1 and 2 to create an account
registration_p1 = True
while registration_p1 == True:
user_1 = input("Enter username for p1")#asks user to create username
password_1 = input("Enter password for p1")#asks user to create password
if user_1 in User_array: # if the details are already found in the text files, the program will inform them
print("That account is already taken, sorry :(")
else:
Users.write(user_1) #saving username to usernames text file
Users.write("\n")
Passwords.write(password_1)#saving password to passwords text file
Passwords.write("\n")
print("Thanks")
registration_1 = False
break
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
def LoggingIn_1(): # function to authenticate player 1
user_1guess = input("Player One, please enter your username: ")
password_1guess = input("Player One, please enter your password: ")
while (user_1guess not in User_array) or (password_1guess not in Password_array):
print("Wrong, closing program ") #if the array cannot find the users supposed log in details it informs them
quit()
user_1guess = input("Player One, please enter your username: ")
password_1guess = input("Player One, please enter your password: ")
else:
player_1 = user_1guess
print ("Hi Player One, I hope you enjoy the game!") #if it is found, the player can move on
User_1 = True
def LoggingIn_2(): #this function does the same for player 2
user_2guess = input("Player Two, please enter your username: ")
password_2guess = input("Player Two, please enter your password: ")
while (user_2guess not in User_array) or (password_2guess not in Password_array):
print("Wrong, closing program ")
quit()
user_2guess = input("Player Two, please enter your username: ")
password_2guess = input("Player Two, please enter your password: ")
else:
player_2 = user_2guess
print ("Hi Player Two, I hope you enjoy the game too!")
User_2 = True
def roll():
Player1Points = 0
Player2Points = 0
points = 0
die1 = random.randint(1,6)
die2 = random.randint(1,6)
dietotal = die1 + die2
points = points + dietotal
if dietotal % 2 == 0:
points = points + 10
else:
points = points - 5
if die1 == die2:
die3 = random.randint(1,6)
points = points +die3
return(points)
for i in range(1,5):
Player1Points += roll()
print('After this round ',player_1, 'you now have: ',Player1Points,' Points')
time.sleep(1)
Player2Points += roll()
print('After this round ',player_2, 'you now have: ',Player2Points,' Points')
time.sleep(1)
if Player1Points == Player2Points:
while Player1Tiebreaker == Player2Tiebreaker:
Player1Tiebreaker = random.randint(1,6)
Player2Tiebreaker = random.randint(1,6)
if Player1Tiebreaker > Player2Tiebreaker:
Player2Points = 0
elif Player2Tiebreaker > Player1Tiebreaker:
Player1Points = 0
if Player1Points>Player2Points:
Winner_Points = Player1Points
winner_User = player_1
winner = (Winner_Points, player_1)
elif Player2Points>Player1Points:
Winner_Points = Player2Points
winner = (Winner_Points, player_2)
winner_User = player_2
print('\nWell done, ', winner_User,' you won with ',Winner_Points,' Point\n\n')
leaderboard1 = "{}:{}".format(Winner_Points,winner_User)
with open ("Leaderboard.txt", "a") as file:
file.write("\n")
file.write(leaderboard1)
f = open('Leaderboard.txt', 'r')
leaderboard = [line.replace('\n','') for line in f.readlines()]
f.close()
leaderboard.sort(reverse=True)
with open('Leaderboard.txt', 'w') as f:
for item in leaderboard:
f.write("%s\n" % item)
print("TOP 5 SCORES:")
with open("Leaderboard.txt", "r") as f:
counter = 0
N = 1
for line in f:
head,sep,tail = line.partition(":")
print("Place No.{}\nUsername: {}Score: {}\n".format(N,tail,head))
N = N + 1
counter += 1
if counter == 5: break
def choices(): # using a function so it is quicker and more efficient, its to output the options to the users and allow them to choose what to do
choices_users = input("Enter 1: to make an account, 2: to log in or 3: to display scores or 4:to play(after you login)")
if choices_users != "1" or "2" or "3" or "4":
print ( "pick a valid option please")
if choices_users == "1": # if they choose "1", the functions for logging in shown previousky are called
registration_p1()
registration_p2()
choices()#then it goes back to the option page
if choices_users == "2":
LoggingIn_1()#the function to sign up is called when "2" is choosen
LoggingIn_2()
choices()#then it goes back to the option page
if choices_users == "3": #this opens the leaderboard and outputs the highest player
scoreboard = open ("scores.txt", "r")
print ("\n")
print("highest player is: ")
scores.close
else:
roll()
choices()