This is a two player dice game where two players each roll two dice. If a player's dice sum is even, they gain 10 points. If the dice total is odd, they lose 5 points.
import random
import time
import sys
print("*****************Welcome To The DICE Game*******************")
abc=input("please type in 'n' if your are a new user type 'e' if you are a existing user: ")
while abc not in ('e','n'):
abc=input("please type in 'n' if your are a new user type 'e' if you are a existing user: ")
if abc =="n":
print("make an account")
username=input("type in a username: ")
password1=input("type in a pasword: ")
password2=input("renter the password: ")
if password1==password2:
print("your password is",password1)
f = open ("username + password.txt","a+")
f.write(f"{username}:{password2}\n")
f.close()
abc=input("please type in 'n' if your are a new user type 'e' if you are a existing user: ")
else:
print("the password you entered is not matching restart the program to start again")
sys.exit()
if abc == "e":
check = True
while check:
print("player 1 enter your details")
username1=input("Username = ")
password=input("Password = ")
with open("username + password.txt","r") as username_finder:
for line in username_finder:
if(username1 + ":" + password) == line.strip():
print("you are logged in")
print("player 2 enter your details")
while check:
print("incoreect password or username please try again")
username2=input("Username = ")
password=input("Password = ")
with open("username + password.txt","r") as username_finder:
for line in username_finder:
if(username2 + ":" + password) == line.strip():
check = False
print("you are logged on")
#game
player1=0
player2=0
print("*****************Welcome To The DICE Game*******************")
print("PLAYER 1 READY")
time.sleep(1)
print("PLAYER 2 READY")
totalscore1=0
totalscore2=0
#player 1
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
roundno = 1
while roundno < 5:
totalscore1=totalscore1+player1
totalscore2=totalscore2+player2
player1=dice1+dice2
roundno=roundno+1
print("round",roundno)
time.sleep(1)
print("-----------------------------------------------------")
asdf = input("player 1, press enter to roll")
print("player 1 is rolling")
print("player 1's first roll is",dice1)
time.sleep(1)
print("player 1's second roll is",dice2)
time.sleep(1)
print("-----------------------------------------------------")
if player1 %2==0:
print("This is an even number. so +10 points")
time.sleep(1)
player1=player1+10
time.sleep(1)
print("score is",player1)
if player1<= 0:
print("you have lost the game")
sys.exit()
print("-----------------------------------------------------")
else:
print("This is an odd number.")
time.sleep(2)
player1=player1-5
print("score is",player1)
time.sleep(3)
print("player 1 score",player1)
print("-----------------------------------------------------")
time.sleep(1)
#player 2
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
totalscore1=totalscore1+player1
totalscore2=totalscore2+player2
player2=dice1+dice2
print("-----------------------------------------------------")
asdf = input("player 2 press enter to roll")
print("player 2 is rolling")
time.sleep(1)
print("player 2's first roll is",dice1)
time.sleep(1)
asdf = input("player 2 press enter to roll again")
time.sleep(1)
print("player 2's second roll is",dice2)
time.sleep(1)
print("-----------------------------------------------------")
if player2 %2==0:
print("This is an even number. so +10 points")
time.sleep(1)
player2=player2+10
print("score is",player2)
time.sleep(1)
if player2<= 0:
print("you have lost the game")
sys.exit()
print("-----------------------------------------------------")
else:
print("This is an odd number.")
time.sleep(1)
player2=player2-5
print("score is",player2)
time.sleep(3)
print("player 2 score",player2)
print("-----------------------------------------------------")
print("the total score for player 1 is ",totalscore1)
print("the total score for player 2 is ",totalscore2)
if totalscore1 > totalscore2:
print("player 1 wins")
file = open("scores.txt2","a+")
file.write("player 1 ")
file.write(username1)
file.write(" has won overall with ")
file.write(str(totalscore1))
file.write(" points")
file.write("\n")
if totalscore2 > totalscore1:
print("player 2 wins")
file = open("scores.txt2","a+")
file.write("player 2 ")
file.write(username2)
file.write(" has won overall with ")
file.write(str(totalscore2))
file.write(" points")
file.write("\n")
else:
print("incorrect username or password")
else:
print("incorrect username or password")
-
2\$\begingroup\$ Is this at all related to codereview.stackexchange.com/q/210517/46840? \$\endgroup\$Carcigenicate– Carcigenicate2019年01月08日 19:42:19 +00:00Commented Jan 8, 2019 at 19:42
-
\$\begingroup\$ no thats my friend we go to the same school \$\endgroup\$blob786– blob7862019年01月08日 20:27:40 +00:00Commented Jan 8, 2019 at 20:27
-
2\$\begingroup\$ Ahh. I knew it looked familiar. And some of the tips mentioned in the other post apply here as well. It's worth reviewing. \$\endgroup\$Carcigenicate– Carcigenicate2019年01月08日 20:28:56 +00:00Commented Jan 8, 2019 at 20:28
-
\$\begingroup\$ I know my friend told me to look at his posts as well \$\endgroup\$blob786– blob7862019年01月08日 20:29:37 +00:00Commented Jan 8, 2019 at 20:29
-
\$\begingroup\$ Try using a password that ends in a space, and then using it to log in. \$\endgroup\$Gloweye– Gloweye2019年09月26日 11:43:12 +00:00Commented Sep 26, 2019 at 11:43
2 Answers 2
This only addresses one aspect of your code, but I think it's an important point to communicate:
Login systems
If you ever actually need a login system, you should investigate cybersecurity and good practices in more depth. Generally, it's advisable to use an existing reputable library or API to accomplish such a task, since it's easy to make mistakes in your own implementation.
Having said that, there are a few things that are easy for me to notice in your code:
- Use
getpass.getpassto prompt the password so others cannot see the entered password. - You should never save passwords in plaintext. Hashing and salting are standard security measures employed in all well-implemented password systems.
- Some other things I mentioned in my comment: it would probably be better to lock-out after too many wrong attempts to prevent brute force attacks, and it should probably prevent registration passwords that have identical hashes to those released in common password data dumps. But these are even more outside of the scope of this question's implicit context.
Of course as @jpmc mentions, this is somewhat less relevant for this particular program, since it stores the password locally. But in general, I think starting to consider the security implications of your implementation is important, even if it's just practice.
-
2\$\begingroup\$ This is all useless considering that it's stored in a plain text file that the user has access to. \$\endgroup\$jpmc26– jpmc262019年01月08日 23:30:54 +00:00Commented Jan 8, 2019 at 23:30
-
\$\begingroup\$ @jpmc26 True, but I really just wanted to make some general points about cybersecurity, hence the caveat need. I also didn't mention that it should probably rate limit for brute force attacks, or prevention of people registering simple passwords are problems. I could, (and will) edit that into my answer. I do agree that Carcigenicate's answer is probably more implementable and immediately useful advice. My intention is communicating a bit of a larger picture idea: cybersecurity is easy to get wrong. \$\endgroup\$Graham– Graham2019年01月08日 23:46:57 +00:00Commented Jan 8, 2019 at 23:46
FUNCTIONS!
Divide your code into functions to make it more readable. In a couple of places, your code is 12 levels of indentation deep! That's the equivalent of 48 characters!
Spacing
Operators should usually have a space on either side.
with
The best way of opening files is as follows (to make sure it get closed even if there is an error):
with open(...) as f:
...
My modified version is available on repl.it.