Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 563 564 [565] 566 567 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882244 times)

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8460 on: November 21, 2015, 06:34:29 pm »

raw_input() returns a string. You need to convert it to an int with a = int(raw_input(...)).
This will crash if the user enters something that's not a number though. Although I could have sworn that your code should crash anyway seeing as you're doing a mathematical comparison with a string.

E: If you want to make the code not crash with the int() function, add a try::except block like so:
Code: [Select]
while True:
    print "How old are you, %s?" %name
    try:
        age = int(raw_input ('>'))
        if age <= 122:
            break
        else:
            print "This doesn't seem right."
    except:
        print "This doesn't seem right."
The interpreter will start running the code in the try: block until it encounters an error (in this case a string entered by the user) and then go to run the code in the except: block.
« Last Edit: November 21, 2015, 06:46:12 pm by Orange Wizard »
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8461 on: November 21, 2015, 06:36:11 pm »

I was going to say something about typing...

Nah, it's probably looking at the memory address or something.

I don't know about Python though.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Magnumcannon

  • Bay Watcher
  • Deep waters don't run still
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8462 on: November 21, 2015, 06:44:22 pm »

raw_input() returns a string. You need to convert it to an int with a = int(raw_input(...)).
This will crash if the user enters something that's not a number though. Although I could have sworn that your code should crash anyway seeing as you're doing a mathematical comparison with a string.
Thanks! You're truly a wizard, Orange Wizard  :D
Logged

Magnumcannon

  • Bay Watcher
  • Deep waters don't run still
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8463 on: November 22, 2015, 04:23:10 pm »

Alright, now i'm trying to use curses on python 2.7. I downloaded UniCurses and i put the pdcurses.dll file in the same folder (site-packages) as unicurses.py. It's not working, for some reason, and everytime i try to import unicurses, i get this:
Spoiler (click to show/hide)
I have no idea what's going on. I tried putting the .dll on the DLLs folder, but it didn't work either.
EDIT: I also found that i could install it with .whl ...but it says it does not support Unicode characters. And i want my ASCII characters  >:(
« Last Edit: November 22, 2015, 04:25:35 pm by Magnumcannon »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8464 on: November 22, 2015, 04:36:56 pm »

How the heck do you use ObjectOutputStream?

E:This is Java.
« Last Edit: November 22, 2015, 05:37:35 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Gatleos

  • Bay Watcher
  • Mournhold... City of Light... City of MAGIC!
    • View Profile
    • Someone Sig This
Re: if self.isCoder(): post() #Programming Thread
« Reply #8465 on: November 22, 2015, 04:52:31 pm »

Is there some specific way I need to initialize JPanels to get them to display properly when I'm not using a layout manager? I need to draw several (overlapping) objects that inherit from JPanel inside a JFrame, but apparently you can only add one child component to a JFrame. So I tried putting my components inside a single JPanel container and adding that to the JFrame, and now it just doesn't render anything at all. I tried setting the size of the container JPanel, but that does nothing.
Logged
Think of it like Sim City, except with rival mayors that seek to destroy your citizens by arming legions of homeless people and sending them to attack you.
Quote from: Moonshadow101
it would be funny to see babies spontaneously combust
Gat HQ (Sigtext)
++U+U++ // ,.,.@UUUUUUUU

Magnumcannon

  • Bay Watcher
  • Deep waters don't run still
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8466 on: November 22, 2015, 04:54:24 pm »

How the heck do you use ObjectOutputStream?
???

Also, i can get Unicurses running on IDLE, but i can't get it running on other shells (I'm using PowerShell). It says that the .DLL wasn't found.
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8467 on: November 22, 2015, 07:05:28 pm »

How the heck do you use ObjectOutputStream?
According to the documentation you just call ObjectOutputStream's writeObject method with an object whose class implements java.io.Serializable.
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8468 on: November 22, 2015, 07:20:02 pm »

Different question, same project, same language (Java):
I need to iterate through an ArrayList to find a unique ID number for a Book object.  It should preferably be the lowest available number, and it must not conflict with any other unique ID number.
Code: [Select]
    public int findUseableBookNumber(ArrayList<Book> list){
        for(Book book: list){
            if(book.getBookNumber() == 0) return findUseableBookNumber(0,list);
        }
        return 0;
    }
    public int findUseableBookNumber(int oldnum, ArrayList<Book> list){
        int testnum;
        testnum = oldnum + 1;
        for(Book book: list){
            if(book.getBookNumber() == testnum) return findUseableBookNumber(testnum, list);
        }
        return testnum;
    }
So, is there some more efficient way of doing this?

getBookNumber(), unsurprisingly, returns the book ID number.
There are two methods because this must check for the availability of zero.

Code: [Select]
    public int findUseableBookNumber(ArrayList<Book> list){
        int testnum = 0;
        int iter;
        boolean didNotFindNumber = true;
        while(didNotFindNumber){
            iter = 0;
            for(Book book: list){
                if(book.getBookNumber() == testnum){
                    testnum++;
                    break;
                }
                iter++;
            }
            if(iter == list.size()) didNotFindNumber = false;
        }
        return testnum;
    }
I think this is much less complex, and does the same thing.


E: Wait, would that fail if it ran into the biggest number at the end of the list?
No.  It would not.
Would it succeed if it actually did run through?  I'm pretty sure that I'm doing this right.  I'm not, however, 100% sure.





How the heck do you use ObjectOutputStream?
According to the documentation you just call ObjectOutputStream's writeObject method with an object whose class implements java.io.Serializable.
Yes, but how do you tell it where to write?
« Last Edit: November 22, 2015, 07:52:13 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

strawberry-wine

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8469 on: November 22, 2015, 11:00:56 pm »

Speaking of Python, anyone here done anything cool with Django? I'm using it for a project soon, feeling a little trepidatious although I've heard it's pretty easy to pick up.
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8470 on: November 23, 2015, 12:18:17 am »

TheBiggerFish, with regards to ObjectOutputStream, one of its constructors takes an OutputStream as a parameter, so for example you could pass a FileOutputStream to the constructor as follows:
Code: [Select]
      FileOutputStream fos = new FileOutputStream(filename);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
Then you can just use the ObjectOutputStream as I described earlier.
As for your problem with the unique ID numbers I think your code would work but it might be more efficient if you had a secondary ArrayList (or other collection) of Book references that was sorted according to ID numbers, and then you could find a free ID number with just one pass through the collection. Incidentally how important is picking the lowest available number?
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8471 on: November 23, 2015, 12:20:03 am »

Not very, but I rather want to.

Ah, I see.  Thanks.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8472 on: November 23, 2015, 01:22:36 am »

Alright, now i'm trying to use curses on python 2.7. I downloaded UniCurses and i put the pdcurses.dll file in the same folder (site-packages) as unicurses.py. It's not working, for some reason, and everytime i try to import unicurses, i get this:
Spoiler (click to show/hide)
I have no idea what's going on. I tried putting the .dll on the DLLs folder, but it didn't work either.
EDIT: I also found that i could install it with .whl ...but it says it does not support Unicode characters. And i want my ASCII characters  >:(
You need to have the pdcurses dll and py in the some folder as the project, or in the Python directory (I think? Maybe not), or somewhere else but pointed to in the Path environment variable.
« Last Edit: November 23, 2015, 01:24:15 am by Orange Wizard »
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Bumber

  • Bay Watcher
  • REMOVE KOBOLD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8473 on: November 23, 2015, 01:44:20 pm »

Speaking of Python, anyone here done anything cool with Django? I'm using it for a project soon, feeling a little trepidatious although I've heard it's pretty easy to pick up.
Nothing cool, but I've used it for a project where we had to display a page with a randomized quote. A large portion of the code was modified from the instructor's example, so I doubt I'd be able to help in any way, other than to dig up my program.
« Last Edit: November 23, 2015, 01:47:10 pm by Bumber »
Logged
Reading his name would trigger it. Thinking of him would trigger it. No other circumstances would trigger it- it was strictly related to the concept of Bill Clinton entering the conscious mind.

THE xTROLL FUR SOCKx RUSE WAS A........... DISTACTION        the carp HAVE the wagon

A wizard has turned you into a wagon. This was inevitable (Y/y)?

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8474 on: November 23, 2015, 01:48:05 pm »

look at the little nerdy car go

http://rednuht.org/genetic_cars_2/

this need to become  game, somehow
I am unsurprised that a weekend of simulation produces cars with more wheel than body, but I can't stop watching.

It reminds me of an old shareware game called Dirt Bike: http://www.youtube.com/watch?v=leGlRL8MFc0

C++ is for writing underlying code, and knowing how to do that in the minimal amount of memory or fastest possible time, not quick hacks that just get the job done without you needing to know what's going on.
As long as you stick to a reasonably safe subset of C++, anyway.  ;)

Ah Python, the language that will keep the C/C++ programmers who write all of the actual fast libraries for it employed for years to come. :P
Or at least until PyPy proves itself faster, more maintainable, and more portable than said libraries...

raw_input() returns a string. You need to convert it to an int with a = int(raw_input(...)).
This will crash if the user enters something that's not a number though. Although I could have sworn that your code should crash anyway seeing as you're doing a mathematical comparison with a string.
That's one of the differences between Python 2 and 3.  In the former, everything was comparable, with some slightly odd defaults for otherwise incomparable objects.  In this case, "22" > 122 because "str" > "int".  Python 3 throws an exception for exactly this reason; clear errors are better than inexplicable misbehavior.

Speaking of Python, anyone here done anything cool with Django? I'm using it for a project soon, feeling a little trepidatious although I've heard it's pretty easy to pick up.
My day job is using it for online micropayments, and I can confirm that it's not hard to use.  For example:  http://pick2give2.org/
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?
Pages: 1 ... 563 564 [565] 566 567 ... 796