Jump to content
Floatplane payments are migrating! Read more... ×
Search In
  • More options...
Find results that contain...
Find results in...

DerpDiamonds1

Member
  • Content Count

    154
  • Joined

  • Last visited

Awards


This user doesn't have any awards

About DerpDiamonds1

  • Title
    Member

Recent Profile Visitors

533 profile views
  1. DerpDiamonds1

    Python CSV help

    Just asking, but what is wrong with my approach? Anyway I finished it and here is the final code: import random import csv import time import sys def storeWinners(winner, winningScore, user1Score, user2Score): file = open("scores.txt", "a") file.write("\n" + winner + ", " + winningScore) file.close() def finalDecide(user1Score, user2Score, user1, user2, i): if user1Score > user2Score: winner = user1 winningScore = str(user1Score) print("*------------------------------------------------------------*") print("The winner for this game is", user1) print("*------------------------------------------------------------*") storeWinners(winner, winningScore, user1Score, user2Score) elif user2Score > user1Score: winner = user2 winningScore = str(user2Score) print("*------------------------------------------------------------*") print("The winner for this game is", user2) print("*------------------------------------------------------------*") storeWinners(winner, winningScore, user1Score, user2Score) elif user1Score == user2Score: print("This is a special occurence as both users have the same score, so we will roll again until one user is higher than the other user!\n") while user1Score == user2Score: time.sleep(1) user1FinalRoll = random.randint(1,6) user2FinalRoll = random.randint(1,6) user1Score = user1Score + user1FinalRoll user2Score = user2Score + user2FinalRoll print("\t For round", i + 2, user1, "got", user1Score, "and", user2, "got", user2Score) i = i + 1 time.sleep(1) if user1Score > user2Score: winner = user1 winningScore = str(user1Score) print("*------------------------------------------------------------*") print("The winner for this game is", user1) print("*------------------------------------------------------------*") storeWinners(winner, winningScore, user1Score, user2Score) elif user2Score > user1Score: winner = user2 winningScore = str(user2Score) print("*------------------------------------------------------------*") print("The winner for this game is", user2) print("*------------------------------------------------------------*") storeWinners(winner, winningScore, user1Score, user2Score) def rollDieWithPoints(user1, user2): print("*------------------------------------------------------------*") print("Now for the main game, please wait as the program calculates each user's score!") print("The scores for each round are as follows, keep in mind that these scores are completely random:\n") user1Score = 0 user2Score = 0 for i in range(0, 5): time.sleep(1) user1Roll1 = random.randint(1,6) user1Roll2 = random.randint(1,6) user2Roll1 = random.randint(1,6) user2Roll2 = random.randint(1,6) user1Score = user1Score + user1Roll1 + user1Roll2 user2Score = user2Score + user2Roll1 + user2Roll2 def user1Points(user1Score): if (user1Score % 2 == 0): user1Score = user1Score + 10 if user1Roll1 == user1Roll2: user1Roll3 = random.randint(1,6) user1Score = user1Score + user1Roll3 elif (user1Score % 2 != 0) and (user1Score - 5 >= 0): user1Score = user1Score - 5 if user1Roll1 == user1Roll2: user1Roll3 = random.randint(1,6) user1Score = user1Score + user1Roll3 else: print("I'm sorry there was an error, please try again later") def user2Points(user2Score): if (user2Score % 2 == 0): user2Score = user2Score + 10 if user2Roll1 == user2Roll2: user2Roll3 = random.randint(1,6) user2Score = user2Score + user2Roll3 elif (user2Score % 2 != 0) and (user2Score - 5 >= 0): user2Score = user2Score - 5 if user2Roll1 == user2Roll2: user2Roll3 = random.randint(1,6) user2Score = user2Score + user2Roll3 else: print("I'm sorry there was an error, please try again later") user1Points(user1Score) user2Points(user2Score) print("\t For round", i + 1, user1, "got", user1Score, "and", user2, "got", user2Score) time.sleep(1) print("\nNow that all the points are accounted for, we can see who won!") finalDecide(user1Score, user2Score, user1, user2, i) def user1Authenticate(): with open('users.txt', mode='r') as file: reader = csv.reader(file) users = [(row[0], row[1]) for row in reader] user1UsernameInput = input("Please input your username: ") user1PasswordInput = input("Please input your password: ") if (user1UsernameInput,user1PasswordInput) in users: print("Correct credentials, you are now logged in with username", user1UsernameInput) return user1UsernameInput elif (user1UsernameInput) not in users: newUsernameInput = input("That username is not in the system, would you like to create a new user by that name? (Y/N): ") if newUsernameInput == "Y": file = open("users.txt", "a") newUsernamePasswordInput = input("Please enter your password?: ") file.write("\n" + user1UsernameInput + "," + newUsernamePasswordInput) file.close() print("New username and password registered, please run the program again!") sys.exit() elif newUsernameInput == "N": print("Okay, wrong username, please try again!") sys.exit() else: print("That is not an input, please try again!") sys.exit() else: print("I'm sorry there was an error, please try again later") sys.exit() def user2Authenticate(): with open('users.txt', mode='r') as file: reader = csv.reader(file) users = [(row[0], row[1]) for row in reader] user2UsernameInput = input("Please input your username: ") user2PasswordInput = input("Please input your password: ") if (user2UsernameInput,user2PasswordInput) in users: print("Correct credentials, you are now logged in with username", user2UsernameInput) return user2UsernameInput elif (user2UsernameInput) not in users: newUsernameInput = input("That username is not in the system, would you like to create a new user by that name? (Y/N): ") if newUsernameInput == "Y": file = open("users.txt", "a") newUsernamePasswordInput = input("Please enter your password?: ") file.write("\n" + user2UsernameInput + "," + newUsernamePasswordInput) file.close() print("New username and password registered, please run the program again!") sys.exit() elif newUsernameInput == "N": print("Okay, wrong username, please try again!") sys.exit() else: print("That is not an input, please try again!") sys.exit() else: print("I'm sorry there was an error, please try again later") sys.exit() def users(): print('User 1, Please log in.') user1 = user1Authenticate() print('User 2, Please log in.') user2 = user2Authenticate() print("Time to play the game!") rollDieWithPoints(user1, user2) def printScores(n): print("*------------------------------------------------------------*") with open('scores.txt', mode='r') as file: reader = csv.reader(file) scores = [(row[0], int(row[1])) for row in reader] scores = sorted(scores, key=lambda score: score[1], reverse=True)[:n] if len(scores) > 0: print("The top {} scores are:\n".format(n)) pos = 1 for name, score in scores: print("\t{}. {} with {}.".format(pos, name, score)) pos += 1 else: print("There are no scores on the leaderboard right now, play the game to earn scores for each user!") def start(): print("*----------* Welcome to my dice rolling game! *----------*") print("Pick an option below to either play the game or check the scores!") startingChoice = input("Do you want to play the game (1) or check scores (2)?: ") if startingChoice == "1": users() elif startingChoice == "2": printScores(5) else: print("That is not an input, please try again") start()
  2. DerpDiamonds1

    Python CSV help

    *bump* as i haven't gotten a response
  3. DerpDiamonds1

    Python CSV help

    So i think this works, but now it won't let me have multiple users: def user1Authenticate(): with open('users.txt', mode='r') as file: reader = csv.reader(file) users = [(row[0], row[1]) for row in reader] user1UsernameInput = input("username: ") user1PasswordInput = input("password: ") print(users[0]) if (user1UsernameInput, user1PasswordInput) in users: print("Correct") return username else: print("CREATE NEW USER") def users(): print('User 1, Please log in.') user1 = user1Authenticate() print('User 2, Please log in.') #user2 = authentication() #print("User 1 is", user1, "and user 2 is", user2) users()
  4. DerpDiamonds1

    Python CSV help

    How do i do that?
  5. DerpDiamonds1

    Python CSV help

    How can i solve that then for multiple users in the file.
  6. DerpDiamonds1

    Python CSV help

    Yes, but how can I solve it.
  7. DerpDiamonds1

    Python CSV help

    What do you mean the structure?
  8. DerpDiamonds1

    Python CSV help

    I was going to do the password verification afterwards, and for the other bit, any user can be assigned to either user1 or user2.
  9. DerpDiamonds1

    Python CSV help

    So let me get straight to the point, my code is supposed to read a .txt file then accept a username and password input form a user and compare it with the input from the user. My current code is: import csv def user1Authenticate(): with open('users.txt', mode='r') as file: reader = csv.reader(file) users = [(row[0], row[1]) for row in reader] user1UsernameInput = input("username: ") user1PasswordInput = input("password: ") print(users[0]) if user1UsernameInput == users[0]: print("Correct") return username else: print("CREATE NEW USER") def users(): print('User 1, Please log in.') user1 = user1Authenticate() print('User 2, Please log in.') #user2 = authentication() #print("User 1 is", user1, "and user 2 is", user2) users() My file's syntax is 'username, password'. Can you find out what is wrong with it? BTW my teacher said we can only user CSV with text file and not .json files or databases. Thanks!
  10. DerpDiamonds1

    Python file multiple users debugging

    Just checking would I go where 'user does not exist' and I would write out the syntax of Jain files, but with their entered username and password?
  11. DerpDiamonds1

    Python file multiple users debugging

    Yea i knew about functions, but i didn't see where to put the part that assigns the username to user1 and user2 once they are authenticated (as that is really my main problem). Thanks and sorry to be such a burden.
  12. DerpDiamonds1

    Python file multiple users debugging

    Thanks, thats great, but what do all the lines in the functions do? Like 'try:' and 'except:' and 'return check_password(data[username], password)'. Sorry to be a burden, i'm not that familiar with .json in python and would really like to learn.
  13. DerpDiamonds1

    Python file multiple users debugging

    So how could I add a part that will make a new user.
  14. DerpDiamonds1

    Python file multiple users debugging

    Thanks thats great, but could this be done with a .txt file. If you could you explain how each line works as I am not familiar wITH .json files in python. Also why .json files over .txt files?
  15. DerpDiamonds1

    Python file multiple users debugging

    Thanks a lot!
×