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

OpenCV - Create multichannel Mat from numpy array
Python urlencode don't encode special characters
Making a sequence of tuples unique by a specific element
Can we make many views.py in Django as a Controller?
What is the status of Functional Reactive Programming in Python?
How to send a request by a private protocol with Python
Django+MongoDb connection error
Odoo/OpenERP failed mail handling
Makefile cannot find module in Python3
How to set up an article input function based on forms in Django
Insert nested value in mongodb using python
Error when installing using pip
How to change python module path
data mining from website using xpath in python
Django: How to prefetch related object with model instance?
How do I resolve error while removing parts of a string in Python DataFrame?

Categories

HOME
sql-server-2008
cil
crate
project-management
textwatcher
firebase-app-indexing
visual-studio-2013
wxwidgets
decorator
tomcat7
checksum
console
requirejs
window
xlsx
abcpdf
cosmicmind
ejs
redux-observable
arraylist
excel-2010
sensu
tcpclient
header-files
bcrypt
guile
pentaho-report-designer
system.data.sqlite
data-conversion
pcre
cell-array
ios-ui-automation
windows-10-iot-core
autocompletetextview
socketpair
declare
image-compression
duktape
dojox.mobile
matlab-cvst
catia
prototypejs
3scale
double-buffering
number-theory
gauss
spell-checking
workflow-foundation-4.5
info
castle-dynamicproxy
addin-express
communication-protocol
visual-studio-templates
build-process
payu
cjson
iostat
push-diffusion
tomee
debugdiag
gitweb
application-loader
code-readability
rad
js-cookie
pyrocms
risk-analysis
graphical-logo
epl
collabnet
spdy
sgml
voldemort
unison
help-viewer
opensocial
winrt-httpclient
healthvault
bsod
driver-signing
va-list
localtime
shiva3d
angularjs-timeout
phpsh
plone-funnelweb
shim
isolatedstorage
supersized
adobe-contribute
drawtext
hardware-acceleration
unreachable-code
delegatecommand
nt4

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