python


sorting through integers in multidimensional array


I am wanting to have the user input a name and highscore, I then want to check if this highscore is greater than any of the high scores in the array. I think the .isdigit() has something to do with it but need further assistance.
HighScores = (("FredROX",43651),("ACE51",41032),("RangiBoi",40080),
("Prince",38987),("DaiZ",35346),("ACE51",35344),("WASSABI",34513),
("MasterG",31095),("RSGuru",30521),("SnipeU",25948))
name = input("Please input a name")
score = int(input("Enter a highscore"))
def Print_Scores():
#Function to print the high score table
#allow function to access HighScores from outside the function
global HighScores
for x in HighScores.isdigit():
if score > HighScores:
HighScores.append(name, score)
print(HighScores)
#call the printing function
Print_Scores()
Rather than this:
for x in HighScores.isdigit():
if score > HighScores:
HighScores.append(name, score)
print(HighScores)
Do this:
for x in HighScores:
if score > x[1]:
HighScores.append(name, score)
print(HighScores)
Remember to make score an int if you are getting input.
isDigit() checks whether a character is a digit.
Make HighScores a list, not a tuple.
Your for loop should probably look something like this. The score is always the second item in each tuple, so you can select that value immediately instead of checking isDigit. You then want to append the name and score in a tuple to that list.
for tup in HighScores:
if score > tup[1]:
HighScores.append((name, score))
Try this:
# import itemgetter
from operator import itemgetter
# HighScores should be a list, not a tuple. That way you can modify it
HighScores = [("FredROX",43651),("ACE51",41032),("RangiBoi",40080),
("Prince",38987),("DaiZ",35346),("ACE51",35344),("WASSABI",34513),
("MasterG",31095),("RSGuru",30521),("SnipeU",25948)]
# cleaned up the input
name = input("Please input a name: ")
score = int(input("Enter a highscore: "))
def Print_Scores():
#Function to print the high score table
#allow function to access HighScores from outside the function
global HighScores
# explained below
if score > max(HighScores, key=itemgetter(1))[1]:
HighScores.append((name, score))
print(HighScores)
#call the printing function
Print_Scores()
The line
if score > max(HighScores, key=itemgetter(1))[1]
deserves explanation.
The itemgetter(1) part is a function that is equivalent to this:
def itemgetter(i):
def f(x):
return x[i]
return f
So itemgetter(1) returns the second item of a sequence.
Now we use that function as the key to max, so that it finds the element that has the greatest score.
max(HighScores, key=itemgetter(1))
Then we take that and get its second item (its score)
max(HighScores, key=itemgetter(1))[1]
And if it's less than the score they entered, we add their score to HighScores.
For future reference, it's probably better if you don't append to the list inside of a function called PrintScores as people will not expect that. I would do it directly after you get the user input, like this:
from operator import itemgetter
HighScores = [("FredROX",43651),("ACE51",41032),("RangiBoi",40080),
("Prince",38987),("DaiZ",35346),("ACE51",35344),("WASSABI",34513),
("MasterG",31095),("RSGuru",30521),("SnipeU",25948)]
name = input("Please input a name: ")
score = int(input("Enter a highscore: "))
if score > max(HighScores, key=itemgetter(1))[1]:
HighScores.append((name, score))
def Print_Scores():
#Function to print the high score table
#allow function to access HighScores from outside the function
global HighScores
print(HighScores)
#call the printing function
Print_Scores()
Of course at that point you don't really need the PrintScores function and you can just print(HighScores)

Related Links

How to create a form for website, without requiring a data-model - Odoo v9 community
Qt setStyleSheet which kind of fonts can I use?
Python matching items accross dataframes
How to add colours to my python code
Flask - Getting a 304 status for my external .css file
User Domains for Office Online
Python - Django - Sum values of a list, too slow
Selenium - Select fields won't save via Remote Webdrive
Find blocks (and their sizes) of same consecutive elements in Pandas
With python dataframes add column of counts of rows that meet condition to each row that meets it
Pandas pivot_table with aggfunc works differently on little different data
`ndimage.distance_transform_bf` transforms the wrong elements?
Generate random words with different lengths
Error in alpha beta prunning algorithm in python
How to do the below task in shell script using grep but without a for loop?
ValueError: could not broadcast input array from shape (97,97) into shape (1)

Categories

HOME
google-apps-script
converter
wildcard
ncurses
nuxeo
uibutton
hyperledger-fabric
simpy
ndis
rfid
android-source
xul
uiscrollview
automata
cratedb
opera-mini
uicollectionview
tree-traversal
boolean-expression
sql-update
apple-tv
piwik
metis
bus-error
interrupt-handling
gzip
pitest
websauna
devforce
jboss5.x
entity-system
graph-databases
preg-grep
rhino
honeysql
jboss-esb
vmd
sonarqube-scan
sql-like
socketscan
ssh.net
fastq
defold
goquery
spell-checking
udev
forerunnerdb
contour
hateoas
java.util.calendar
registrykey
retro-computing
ibm-data-studio
musl
utf-32
pgagent
alphablending
adcolony
google-admin-audit-api
collabnet
log4cplus
funq
voldemort
roxygen
baucis
sim900
logentries
modeshape
dllexport
localtime
marmalade-edk
attachevent
moq-3
fotoware
wcf-web-api
unions
selectmanycheckbox
opengl-es-lighting
noir
ihttphandler
zend-form-element
pureftpd
clause
open-graph-beta
asp.net-mvc-controller
configurable

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App