# leaderboard.py # The leaderboard module to be used in Activity 1.2.2 # set the levels of scoring bronze_score = 15 silver_score = 20 gold_score = 25 # return names in the leaderboard file def get_names(file_name): leaderboard_file = open(file_name, "r") # be sure you have created this # use a for loop to iterate through the content of the file, one line at a time # note that each line in the file has the format "leader_name,leader_score" for example "Pat,50" names = [] for line in leaderboard_file: leader_name = "" leader_score = "" index = 0 # TODO 1: use a while loop to read the leader name from the line (format is "leader_name,leader_score") while (line[index] != ","): leader_name = leader_name + line[index] index = index + 1 print("leader name is:", leader_name) # TODO 2: add the player name to the names list leader_names.append(leader_name) leaderboard_file.close() # return scores from the leaderboard file def get_scores(file_name): leaderboard_file = open(file_name, "r") # be sure you have created this scores = [] for line in leaderboard_file: leader_score = "" index = 0 # TODO 3: use a while loop to index beyond the comma, skipping the player's name index = index +1 while (line[index] != "\n"): leader_score = leader_score + line[index] index = index + 1 print("Score is:", int(leader_score)) # TODO 4: use a while loop to get the score leader_scores.append(leader_score) # TODO 5: add the player score to the scores list # TODO 6: return the names list in place of the empty list return [] leaderboard_file.close() # TODO 7: return the scores in place of the empty list return [] # update leaderboard by inserting the current player and score to the list at the correct position def update_leaderboard(file_name, leader_names, leader_scores, player_name, player_score): index = 0 # TODO 8: loop through all the scores in the existing leaderboard list ''' for : # TODO 9: check if this is the position to insert new score at if (): break else: index = index + 1 ''' # TODO 10: insert new player and score # TODO 11: keep both lists at 5 elements only (top 5 players) # TODO 12: store the latest leaderboard back in the file ''' leaderboard_file = open(file_name, "w") # this mode opens the file and erases its contents for a fresh start # TODO 13 loop through all the leaderboard elements and write them to the the file for : leaderboard_file.write(leader_names[index] + "," + str(leader_scores[index]) + "\n") leaderboard_file.close() ''' # draw leaderboard and display a message to player def draw_leaderboard(high_scorer, leader_names, leader_scores, turtle_object, player_score): # clear the screen and move turtle object to (-200, 100) to start drawing the leaderboard font_setup = ("Arial", 20, "normal") turtle_object.clear() turtle_object.penup() turtle_object.goto(-160,100) turtle_object.hideturtle() turtle_object.down() # loop through the lists and use the same index to display the corresponding name and score, separated by a tab space '\t' for index in range(len(leader_names)): turtle_object.write(str(index + 1) + "\t" + leader_names[index] + "\t" + str(leader_scores[index]), font=font_setup) turtle_object.penup() turtle_object.goto(-160,int(turtle_object.ycor())-50) turtle_object.down() # move turtle to a new line turtle_object.penup() turtle_object.goto(-160,int(turtle_object.ycor())-50) turtle_object.pendown() # TODO 14: display message about player making/not making leaderboard turtle_object.write("Congratulations!\nYou made the leaderboard!", font=font_setup) turtle_object.write("Sorry!\nYou didn't make the leaderboard.\nMaybe next time!", font=font_setup) # move turtle to a new line turtle_object.penup() turtle_object.goto(-160,int(turtle_object.ycor())-50) turtle_object.pendown() # TODO 15: Display a gold/silver/bronze message if player earned a gold/silver/or bronze medal; display nothing if no medal turtle_object.write("You earned a gold medal!", font=font_setup) turtle_object.write("You earned a silver medal!", font=font_setup) turtle_object.write("You earned a bronze medal!", font=font_setup)
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |