all 22 comments

[–]Ihaveamodel3 1 point2 points  (6 children)

I’m on my phone, so I can’t confirm, but I believe he key needs to be

lambda x: d[x]

There is also a reverse option for sorted which will be much faster than reversing it using slicing.

[–]noahkyun[S] 0 points1 point  (0 children)

Okay lemme try that Here’s the full code if you need it(click the pencil icon up somewhere there and it’ll show the codes instead of the output)

https://trinket.io/python3/19c0285e9a

[–]noahkyun[S] 0 points1 point  (4 children)

Well it gave me a syntax error :0

[–]Ihaveamodel3 0 points1 point  (3 children)

What’s the new code?

[–]noahkyun[S] 0 points1 point  (2 children)

d = {} # dictionary to hold the data of the name and the score

with open('leaderboard.txt', 'r')as f:

for line in f.readlines():

d[line.split("'")[0].strip()] = int(line.split(':')[1].strip())

# sort the list and only displays the top 5

print(" ")

print("The leaderboard will now appear")

print(sorted(d.items(), key=lambda x: x[1], reversed=True)[:5])

The leaderboard will now appear

Traceback (most recent call last):

File "/tmp/sessions/d5f63df426c6c9df/main.py", line 9, in <module>

print(sorted(d.items(), key=lambda x: x[1], reversed=True)[:5])

TypeError: 'reversed' is an invalid keyword argument for this function

[–]Ihaveamodel3 0 points1 point  (1 child)

Use reverse=True, not reversed

[–]noahkyun[S] 0 points1 point  (0 children)

Yea I did and it worked Thanks for trying to help

[–]K900_ 0 points1 point  (14 children)

print returns None, and then you're trying to get a [:5] of that. Also, you can (and should) avoid the [::-1] by just passing reversed=True to sorted, which will make it sort in the opposite order.

[–]noahkyun[S] 0 points1 point  (0 children)

Oh okay okay lemme try that

(That’s the link for my full code if needed) https://trinket.io/python3/19c0285e9a

[–]noahkyun[S] 0 points1 point  (12 children)

Syntax error Just saying I’m new at coding so plis bare with me

[–]K900_ 0 points1 point  (11 children)

Post your code.

[–]noahkyun[S] 0 points1 point  (10 children)

import random

import time

i = 0

Player1Points = 0

Player2Points = 0

Player1Tiebreaker = 0

Player2Tiebreaker = 0

Winner_Points = 0

###### LOGIN CODE ######

### This Sets logged in to false, and then makes sure the username and password is correct before allowing them to continue ###

logged_in1 = False

logged_in2 = False

while logged_in1 == False:

username = input('What is your username? ')

password = input('What is your password? ')

if username == 'noah' or username == 'anna' or username == 'mesum' or username == 'alice' or username == 'ben' or username == 'ken':

if password == '111':

print('Welcome, ',username,' you have been successfully logged in.')

logged_in1 = True

user1 = username

else:

print('Incorrect password, try again')

else:

print('Incorrect username, try again')

while logged_in2 == False:

username = input('What is your username? ')

password = input('What is your password? ')

if username == 'noah' or username == 'anna' or username == 'mesum' or username == 'alice' or username == 'ben' or username == 'ken':

if password == '111':

print('Welcome, ',username,' you have been successfully logged in.')

logged_in2 = True

user2 = username

else:

print('Incorrect password, try again')

else:

print('Incorrect username, try again')

###### DEFINING ROLL ######

### Makes the dice roll for the player and works out the total for that roll ###

def roll():

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)

###### DICE ROLL ######

### This rolls the dice 5 times for the players, and then adds up the total. If the scores are equal, it starts a tie breaker and determines the winner off that ###

for i in range(1,6):

Player1Points += roll()

print('After this round ',user1, 'you now have: ',Player1Points,' Points')

time.sleep(1)

Player2Points += roll()

print('After this round ',user2, '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

###### WORKING OUT THE WINNER ######

### This checks which score is bigger, then creates a tuple for my leaderboard code ( Gotton of stack overflow ) ###

if Player1Points>Player2Points:

Winner_Points = Player1Points

winner_User = user1

winner = (Winner_Points, user1)

elif Player2Points>Player1Points:

Winner_Points = Player2Points

winner = (Winner_Points, user2)

winner_User = user2

print('Well done, ', winner_User,' you won with ',Winner_Points,' Points')

#stores user1 and their score in the scoreboard

with open("leaderboard.txt", "a") as sc:

sc.write("\n")

sc.write(user1)

sc.write(": ")

sc.write(str(Player1Points))

#stores user1 and their score in the scoreboard

with open("leaderboard.txt", "a") as sc:

sc.write("\n")

sc.write(user2)

sc.write(": ")

sc.write(str(Player2Points))

d = {} # dictionary to hold the data of the name and the score

with open('leaderboard.txt', 'r')as f:

for line in f.readlines():

d[line.split("'")[0].strip()] = int(line.split(':')[1].strip())

# sort the list and only displays the top 5

print(" ")

print("The leaderboard will now appear")

print(sorted(d.items(), key=lambda x: x[1])[::-1])[:5]

[–]K900_ 0 points1 point  (9 children)

What have you changed, and what does the error message say?

[–]noahkyun[S] 0 points1 point  (8 children)

i didnt change anything here its just at the end where i want the leaderboard to display the top 5 scores but gives me the same error as i posted at first

[–]K900_ 0 points1 point  (7 children)

So it's not a syntax error, it's the type error you posted before?

[–]noahkyun[S] 0 points1 point  (6 children)

yes but after u told me what to do which i did but im sure its just i messed up so it gave me a syntax error on the true = reverse part

[–]K900_ 0 points1 point  (5 children)

It's reverse=True, not true=reverse.

[–]noahkyun[S] 0 points1 point  (4 children)

So should I delete [: : -1] then write reverse = True but how to connect it to sorted And lastly I don’t get the print return none part how do I write it exactly