Having trouble copying highscores onto txt file

This is the place for queries that don't fit in any of the other categories.

Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Sat Dec 07, 2013 7:04 pm

Hi. I have been having trouble copying the score onto
my highscores file when the score is greater than than
the contents of my highscores file - eccept it ignores the
part of the code that makes it do that. I don't know why
it skips that block of code. It doesn't when I swich the
if score >= readit: to if score <= readit:.
Could anyone help with this? Here is the block of code I'm working on:

Code: Select all
    if key[pygame.K_SPACE] and flyswatter_rect.colliderect(bug_rect):
        place = [(800,500) , (800,80) , (800,200) , (800,298)]
        setplace = random.choice(place)
        bug_rect = bug_image.get_rect(topleft=(setplace))
        level += 0.5
        score += 1
        Flyswatter_hit = pygame.transform.rotate(Flyswatterhit, 290)
        DISPLAYSURF.blit(Flyswatter_hit, flyswatter_rect)
        DISPLAYSURF.blit(bug_image, bug_rect)
       
    r = open("Output.txt", "r")
    readit = r.read()
    if score >= readit(): #this is the part that skips
        r = open("Output.txt", "r")
        readit = r.read()
        w = open("Output.txt", "w")
        w.write("{}".format(score))

       
    if p:   
        scoreboard = myfont.render('Score: {}'.format(score), 1 , BLACK)
        DISPLAYSURF.blit(scoreboard, (10,10))
       
    highscore = myfont.render('Highscore:{}'.format(readit), 1 , BLACK)
    DISPLAYSURF.blit(highscore, (650, 10))
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Having trouble copying highscores onto txt file

Postby metulburr » Sat Dec 07, 2013 8:28 pm

Code: Select all
    r = open("Output.txt", "r")
    readit = r.read()
    if score >= readit(): #this is the part that skips

score looks to be of type int, while readit is surely type string as open().read() returns a string. You need to convert readit to type int before comparison.

This is something you should of encountered way before starting 2d game programming. But your not going to listen anyways.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Sat Dec 07, 2013 9:40 pm

Hey, I'll work on it.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Sat Dec 07, 2013 10:13 pm

Okay, this is not going to be as easy as I
thought. int(readit) won't work. it just
returns this error:

Code: Select all
ValueError: invalid literal for int() with base 10: ''


How would I do this?
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Having trouble copying highscores onto txt file

Postby metulburr » Sun Dec 08, 2013 3:37 am

If its a float in the file, then you need to convert it to a float and not an int
float(readit)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Sun Dec 08, 2013 3:21 pm

it says it can't convert it:

Code: Select all
ValueError: could not convert string to float:
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Having trouble copying highscores onto txt file

Postby Mekire » Sun Dec 08, 2013 3:26 pm

You really aren't making it easy to help you. Could you please print the string before trying to convert it and tell us exactly what it prints. Does your file even contain an initial value (hint: in this case it better)?

-Mek

Edit:
Code: Select all
if score >= readit()
Why does readit have parenthesis after it if it is supposed to be a string (or preferably an integer).
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Sun Dec 08, 2013 5:39 pm

Mekire wrote:Could you please print the string before trying to convert it and tell us exactly what it prints.

I am trying to print the number in output.txt(0) with
print int(readit). It prints 0, except
when I go if score >= int(readit):, it
brings up the error.

Mekire wrote:
Code: Select all
if score >= readit()
Why does readit have parenthesis after it if it is supposed to be a string (or preferably an integer).


Typo. sorry.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Having trouble copying highscores onto txt file

Postby stranac » Sun Dec 08, 2013 6:04 pm

Hissing Cockroach wrote:Okay, this is not going to be as easy as I
thought. int(readit) won't work. it just
returns this error:

Code: Select all
ValueError: invalid literal for int() with base 10: ''



Looks like the value of readit is an empty string... Is your file empty?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Sun Dec 08, 2013 7:14 pm

no.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth

Re: Having trouble copying highscores onto txt file

Postby metulburr » Sun Dec 08, 2013 7:23 pm

Mekire wrote: Could you please print the string before trying to convert it and tell us exactly what it prints.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Having trouble copying highscores onto txt file

Postby Mekire » Mon Dec 09, 2013 7:36 am

Hissing Cockroach wrote:no.
Perhaps we need to forgo the subject of programming entirely.

Please read this thoroughly and tell me which maxims you violate and why:
http://en.wikipedia.org/wiki/Cooperative_principle

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Having trouble copying highscores onto txt file

Postby Hissing Cockroach » Mon Dec 09, 2013 5:23 pm

Okay, when I go

Code: Select all
print int(readit)


it says:

Code: Select all
0


which is in my file.
User avatar
Hissing Cockroach
 
Posts: 108
Joined: Sat Sep 14, 2013 2:35 pm
Location: Earth


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests