By blob786


2019-01-08 17:47:29 8 Comments

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 comments

@Graham 2019-01-08 18:39:43

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.getpass to 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.

@jpmc26 2019-01-08 23:30:54

This is all useless considering that it's stored in a plain text file that the user has access to.

@Graham 2019-01-08 23:46:57

@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.

@Solomon Ucko 2019-01-08 19:53:17

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.

@colkat406 2019-01-09 16:06:05

could you check my code and modify it if anything is wrong please [codereview.stackexchange.com/questions/210517/…

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] 2-player dice game

2 Answered Questions

[SOLVED] Two-player dice game for NEA task computer science (Updated)

3 Answered Questions

[SOLVED] Python login system for a dice game

1 Answered Questions

[SOLVED] Dice game login system

  • 2018-12-15 20:03:58
  • Krishna
  • 60 View
  • 1 Score
  • 1 Answer
  • Tags:   python

1 Answered Questions

[SOLVED] 2 player dice game

1 Answered Questions

[SOLVED] Simple Version of Bunco Game

  • 2016-07-29 06:32:29
  • user302686
  • 986 View
  • 5 Score
  • 1 Answer
  • Tags:   beginner c game dice

1 Answered Questions

[SOLVED] Dice-throwing game

  • 2016-04-26 13:19:28
  • Thor
  • 2737 View
  • 5 Score
  • 1 Answer
  • Tags:   java dice

3 Answered Questions

[SOLVED] simple two players dice throwing game

  • 2016-04-19 13:55:04
  • Thor
  • 17247 View
  • 6 Score
  • 3 Answer
  • Tags:   java dice

1 Answered Questions

[SOLVED] Player vs computer dice game

  • 2016-03-06 12:36:41
  • RajVader
  • 1415 View
  • 2 Score
  • 1 Answer
  • Tags:   java dice

1 Answered Questions

[SOLVED] Pig dice game with a human and a computer player

  • 2015-11-16 22:44:15
  • blackened
  • 3846 View
  • 3 Score
  • 1 Answer
  • Tags:   python game dice

Sponsored Content