Day 049 - Project 49 : Loading a High Score Table

If you have any questions, comments or issues with this project please post them here!

Hello, I’m having an error when trying to load the high.scores file from Day 49 on 100 days of code(for Python). Everything appears to be ok and I’m not sure what I’m doing wrong. I can link my replit to the forum for help.

1 Like

You must make a file named high.score. Also, welcome to community @sonicx180 !

you beat me again. Next time, mention me at the top of your post :slight_smile:

2 Likes

Hi! Thank you for welcoming me to the community and for the quick reply. I’m still not clear about what I’m doing wrong.

https://replit.com/@Kittycat007/Day49100Days?v=1

I thought I loaded the file, but I’m still getting an error message about it loading.

yes, you just need to make a file, try press the file icon that has a plus in it, and then make a file named high.score and put a bunch of numbers (scores) in it :smiley:

like this:

John Smith 100
John Doe 200

EDIT: reveal this by closing the tutorial and pressing the reveal sidebar
image


2 Likes

OK. I’ll try again. I understand that in theory but it hasn’t been working with the code. This is the first time I’ve gotten stuck.

1 Like

Hey there, the code provided is wrong, you are correct! Try this:

import os, time

f = open("high.score", "r")
scores = f.read().split("\n")
f.close()

highscore = 0
name = None

for rows in scores:
  data = rows.split()
  if data != []:
    if int(data[-1]) > highscore:
      highscore = int(data[-1])
      name = " ".join(data[:-1])

print("The winner is", name, "with", highscore)

Here: you can fork

1 Like

I managed to fix it!

Thank you for the feedback and the new code, it was very helpful. I’m completely self-taught and I’m sure my process would be faster if I asked for more help along the way. Getting help was easier than I expected it to be. Thanks again!!

1 Like

you’re welcome, hope you enjoy the community :smiley:

1 Like

Hi, not sure if this is a bug or I’m doing something wrong. I’ve opened the high.score file that was provided, you can see it open on the bottom tab but whenever I try to run the code, it says no such file or directory.

the thing has the wrong code sadly, to fix this either:

  1. or just put this in main.py:
f = open(".tutorial/high.score", "r")
  1. create a NEW file called high.score and put:
DMO 20332
K8T 398398398938
BRI 323232
SAM 494949

like this:

2 Likes

I am struggling with this project. and cannot remember how the index and Len() function work. Could someone please give me a refresher on this in a way I can remember it.

Here’s an example:

myList = ["x", "y", "z"]

We created a list with 3 items.

The len function takes an iterable and returns the number of elements in it. You can remember it as “len” is short for “length” and the function returns the length of the iterable.

print(len(myList)) # This outputs 3 because there are 3 elements in the list

And index returns the index of an element in the list.

print(myList.index("x")) # This outputs 0 because list indices start at 0 and "x" is the first item we defined in the list
3 Likes

I know what .split() does but what does .split("\n") do?

\n represents a newline character, so it does the same as.split() except it splits by newline characters.

4 Likes

can you please explain 3-5 lines?

i cant understand how he compare the numbers with that method i mean when he wrote (highscore=0) and (if data[1]>highscore:)like data is always biger than 0 but it worked so well i cant got it please explain?>.

Got it finally after some head scratching but it looks different to the Solution (No Peeking!):

Forgive me if these are not the lines you are referring to:

3. f = open("high.score", "r")       #this is opening the  read-only file called high.score
4. scores = f.read().split("\n")     #this is creating a variable which reads the file, splits the contents and goes to the next line
5. f.close()                                         #this closes the file
1 Like