Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 529 530 [531] 532 533 ... 796

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

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7950 on: September 24, 2015, 03:46:15 pm »

Really, for programming there's mainly a basic concept you have to grasp that is portable to almost any language.

And then there's a whole lot of best practices and good habits, and more advanced things such as OO.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7951 on: September 24, 2015, 03:52:10 pm »

And then there's all the different kinds of declarative programming, where that portable basic concept you're talking about doesn't really apply.
Logged
Taste my Paci-Fist

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7952 on: September 24, 2015, 03:55:15 pm »

Which is why I said almost any instead of all.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

itisnotlogical

  • Bay Watcher
  • might be dat boi
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7953 on: September 24, 2015, 11:58:53 pm »

Here's a video about punch card programming. Considering that retro programming manuals are what interested me in programming in the first place, this was highly interesting to me.
Logged
This game is Curtain Fire Shooting Game.
Girls do their best now and are preparing. Please watch warmly until it is ready.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #7954 on: September 25, 2015, 07:45:40 am »

Is there a way to figure out which command you are using to run your Python interpreter? Eg, if the code for that was in file called find_my_name.py, it would be like:

$ python find_my_name.py
python
$ python3 find_my_name.py
python3

Reason is, I want a python script that calls a different python script, captures its stdout output and do stuff with it. But I know that I use python3 for my own machine, and I have no idea what my TA uses!
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Arx

  • Bay Watcher
  • Iron within, iron without.
    • View Profile
    • Art!
Re: if self.isCoder(): post() #Programming Thread
« Reply #7955 on: September 25, 2015, 07:52:17 am »

You can use sys.version_info for that, I think.
Logged

I am on Discord as Arx#2415.
Hail to the mind of man! / Fire in the sky
I've been waiting for you / On this day we die.

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7956 on: September 25, 2015, 03:19:08 pm »

Is there a way to figure out which command you are using to run your Python interpreter?

Looks like sys.executable is exactly what you need.  It even seems to work inside a virtual environment.

Reason is, I want a python script that calls a different python script, captures its stdout output and do stuff with it. But I know that I use python3 for my own machine, and I have no idea what my TA uses!

Sounds trickier than it needs to be, but I can recommend the subprocess library for such tasks.
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7957 on: September 25, 2015, 04:27:19 pm »

Oh well. C++ to me is either really great once you can get to understanding it or a pain with all of its ultra precison in complicated synthax. I.E

cout <<
cin >>

I learned the signals by heart imagining a racecars going outside a tunnel or inside with those two. Not hard but when you add..

pow(x,y) where x and y can be heavilly modified and can have extra parenthesis.

Nesting If's has also been troublesome because I was given the logic to 'connect the if's like you would blocks' instead of stuffing them in together.

Previous believed this:

Block of if 1
--------------------separated but connected.
Block of if 2.

Instead of:

Block 1 (

Block 2()

)

I'm trying to think of a good way to explain C syntax, but you're right, it gets tricky.  Statements can be blocks of statements, expressions can contain sub-expressions, and so forth, even before getting into operator overloading tricks, class definitions, templates, and macros.

But to start with the basics, your function body is a list of statements, which the computer executes one by one.  Each statement can be a variable declaration, an expression, an if statement, a while statement, a block of statements, or a few other things that could be expressed in those terms.  (Okay, so variable declarations are a bit wiggly as far as when they get executed.  Ignore that for now.)  For example:

  • int a = 3;
  • f(a);
  • if (a > 4) g();
  • while (a < 10) a = h(a);
  • { f(a); g(); }

That last item, the block surrounded by braces, acts much like the function body itself; it contains a list of statements, executed in order, one by one.  Notably, the statements in a block can be any of the above statement types, even other blocks.

Expressions consist of operators applied to names and/or other expressions, much like mathematical notation.  Granted, C has special meanings for certain operators, sometimes in particular places; for example, a single equal sign (=) indicates assignment, not comparison.  From your example, "cout << f" applies the << operator to cout and f, which the cout object takes to mean that it should print f to the screen.

The if statement is interesting, because it requires an expression and one or two other statements:

Code: [Select]
if (expression) true_statement else false_statement
When the computer gets to this statement, it evaluates expression.  If (and only if) the result is true (non-zero), then true_statement is executed.  Otherwise, false_statement is executed.  (The "else false_statement" part can be omitted, in which case nothing is executed if the expression is false.)  In either case, the computer goes on to execute the next statement in the surrounding block.

Frequently, the expressions for the if statement are blocks.  Also common is a second if statement as the false_statement.

Code: [Select]
if (a > 10) {
    if (b > a) f();
    if (b < 15) g();
} else if (a > 5) {
    h();
} else {
    q();
    if (b < a) {
        r();
    }
}

if (b > 10) {
    s();
}

t();

This snippet has examples of if statements nested inside each other, and examples of if statements after each other.  Which functions will be called for various values of a and b?  As a particularly interesting example, what will be called for a = 14, b = 12?

But perhaps this will all be easier to understand once you learn about recursion...

And yes, C++ is either really great or a massive pain.  When I want the level of control it requires me to exert, I prefer straight C.  When I want the power of objects and classes, I prefer Python.  There are good reasons to use C++, but if it's not obvious, something else is probably better.
Logged
Quote from: Kevin Wayne, in r.g.r.n
Is a "diety" the being pictured by one of those extremely skinny aboriginal statues?

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7958 on: September 25, 2015, 04:34:28 pm »

You should always use blocks instead of single expressions, really.
It prevents weird and hard to debug errors.

Also, true and non-zero are not the same thing.
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

Dutrius

  • Bay Watcher
  • No longer extremely unavailable!
    • View Profile
    • Arcanus Technica
Re: if self.isCoder(): post() #Programming Thread
« Reply #7959 on: September 25, 2015, 04:54:18 pm »

I decided to override the ToString function for my inventory class.

inventory.ToString() now returns a string containing the names, quantities and combined mass of all the items.

Code: [Select]
public override string ToString()       //returns names, quantities and  and combined mass of all items in inventory.
{
    string returnString = "Contents:";
    for (int i = 0; i < items.Length; i++)
    {
         returnString += (" "+items[i].quantity.ToString()+" x "+items[i].itemName);
         if (i < items.Length - 1)
         {
              returnString += ",";
         }
    }
    returnString += (". Combined mass: "+combinedMass.ToString()+"kg.");
    return returnString;
}
Logged
No longer extremely unavailable!
Sig text
ArcTech: Incursus. On hold indefinitely.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7960 on: September 25, 2015, 05:14:55 pm »

I decided to override the ToString function for my inventory class.

inventory.ToString() now returns a string containing the names, quantities and combined mass of all the items.

Code: [Select]
public override string ToString()       //returns names, quantities and  and combined mass of all items in inventory.
{
    string returnString = "Contents:";
    for (int i = 0; i < items.Length; i++)
    {
         returnString += (" "+items[i].quantity.ToString()+" x "+items[i].itemName);
         if (i < items.Length - 1)
         {
              returnString += ",";
         }
    }
    returnString += (". Combined mass: "+combinedMass.ToString()+"kg.");
    return returnString;
}
Where's the @Override?
And I don't THINK (emphasis on think) that override is a thing that goes in the method header?  Anyone else?
(On phone, thus no can google properly.)
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.

RoguelikeRazuka

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7961 on: September 25, 2015, 05:17:00 pm »

Hey what do you say about Ruby? I've tried it today, and after C/C++ and Pascal it feels like a blessing. Though what programs are commonly written in Ruby?
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7962 on: September 25, 2015, 05:18:13 pm »

It's a scripting language, I've worked a bit in it for some DFHack stuff (since DFHack has ruby support).

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7963 on: September 25, 2015, 05:34:23 pm »

I'm agreeing with TheBiggerFish, if he's getting at what I think he is.

You don't use the override keyword if you're making classes from scratch. It's merely coincidence that you picked ToString as a method name, and the compiler does not logically connect this with other things called "ToString" defined in unrelated classes. e.g.

Code: [Select]
class Bob
{
    void ToString();
}

^ this is not an "override" of the ToString method of e.g. the "int" type, since "Bob" is not a subclass of "int". You're only overriding things that were received through inheritance. ToString has no meaning outside of the class/namespace where it was defined, so you're free to make functions, methods or even variables called "ToString" and they won't conflict anything else.
« Last Edit: September 25, 2015, 05:42:32 pm by Reelya »
Logged

breadman

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #7964 on: September 25, 2015, 05:43:01 pm »

Hey what do you say about Ruby? I've tried it today, and after C/C++ and Pascal it feels like a blessing. Though what programs are commonly written in Ruby?

It's approximately equivalent to Python.  Slightly more powerful syntax, but less readable.  Slightly newer, slightly slower, slightly more open to monkey-patching.

The main reasons to learn Ruby are Rails (web framework) and Puppet (configuration management).  It's general-purpose enough to handle anything, though, from short scripts to full applications.  It and Python are great choices for anything that doesn't clearly fall into another language's niche.
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 ... 529 530 [531] 532 533 ... 796