Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. import time
  2. from random import randint
  3.  
  4. def registration():
  5.     username=input("Create a username.")
  6.     password=input("Create a password.")
  7.     myFile=open("authentication.txt", "a+")
  8.     myFile.write(username + "," + password + "\n")
  9.     myFile.close()
  10.     print("Registration successful.")
  11.  
  12.  
  13. def login():
  14.     file=open("authentication.txt", "r")
  15.     logins=file.read()
  16.     username=input("Enter your username.")
  17.     password=input("Enter your password.")
  18.     authorised=False
  19.     while True:
  20.         if (username+ "," +password) not in logins:
  21.             print("Login unsuccessful. Type exit to exit")
  22.             username=input("Enter your username.")
  23.             password=input("Enter your password.")
  24.         else:
  25.             authorised=True
  26.             break
  27.         if username=="exit" or password=="exit":
  28.             return False
  29.     if authorised:
  30.         print("Login successful.")
  31.         return True
  32.  
  33.  
  34. def playerturns():
  35.     totalscore=0
  36.     num1=randint(1, 6)
  37.     num2=randint(1, 6)
  38.     totalscore=num1+num2
  39.     if totalscore<0:
  40.         totalscore=0
  41.     if num1==num2:
  42.         totalscore+=randint(1, 6)
  43.     if(num1+num2)%2==0:
  44.         totalscore=totalscore+10
  45.     else:
  46.         totalscore=totalscore-5
  47.     print("Rolled",num1,",",num2)
  48.     print(totalscore, "points added to your score")
  49.     return totalscore
  50.  
  51.  
  52.  
  53. p1total=0
  54. p2total=0
  55.  
  56. registration()
  57. login()
  58.  
  59.  
  60.  
  61. for i in range(0,5):
  62.     print("Player 1's turn!")
  63.     p1total=p1total+playerturns()
  64.     print("Player 1's score is", p1total)
  65.     input("Press enter to roll the die")
  66.     print("\n"*10)
  67.     print("Player 2's turn!")
  68.     p2total=p2total+playerturns()
  69.     print("Player 2's score is", p2total)
  70.     input("Press enter to roll the die")
  71.     print("\n"*10)
  72.  
  73.  
  74. while p1total==p2total:
  75.     p1num=randint(0, 6)
  76.     p2num=randint(0, 6)
  77.     p1total=p1total+p1num
  78.     p2total=p2total+p2num
  79.  
  80. if p1total>p2total:
  81.     print("Player1 wins." + "\n" + "Player1TotalPoints: " + str(p1total) + "\n" + "Player2TotalPoints: " + str(p2total))
  82. elif p1total<p2total:
  83.     print("Player2 wins." + "\n" + "Player1TotalPoints: " + str(p1total) + "\n" + "Player2TotalPoints: " + str(p2total))
  84.  
  85. if p1total>p2total:
  86.     p1name=input("Player1, enter your name.")
  87.     myFile=open("scores.txt", "a")
  88.     myFile.write(str(p1name)+ "," +str(p1total)+ "\n")
  89.     myFile.close()
  90. elif p1total<p2total:
  91.     p2name=input("Player2, enter your name.")
  92.     myFile=open("scores.txt", "a")
  93.     myFile.write(str(p2name)+ "," +str(p2total)+ "\n")
  94.     myFile.close()
  95.  
  96.  
  97. myFile=open("scores.txt","r")
  98. pscores=[line.rstrip() for line in myFile]
  99.  
  100. def bubblesort(pscores):
  101.     swapsmade=True
  102.     while swapsmade==True:
  103.         swapsmade=False
  104.         for i in range(0, len(pscores)-1):
  105.             if int(pscores[i][1])<int(pscores[i+1][1]):
  106.                 temp=pscores[i]
  107.                 pscores[i]=pscores[i+1]
  108.                 pscores[i+1]=temp
  109.                 swapsmade=True
  110.     for i in range (0,5):
  111.         print(pscores[i])
  112.     return pscores
  113.  
  114. bubblesort(pscores)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement