Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 336 337 [338] 339 340 ... 796

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

Killjoy

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5055 on: September 28, 2013, 01:40:51 pm »

That's not really the problem...it's that I have no idea how to turn my .py files into .exes from Notepad++, really, so I can debug and goof around with them. I installed the plugin NppExec, and I don't really understand it.
If you turn your python into a executable you will have the same problem. Windows simply kills the console whenever the program has exited.

Honestly the way that windows assigns a console window to applications is weird and pretty dumb. On more reasonable OSes(.*inx systems), standard output is ignored unless the user specifies where the output should go, for an example the shell(Console).
On windows the OS will for whatever reason give the program a console window if it detects standard output (I am a little fuzzy on the details, but I think this is how it works). Since your program exits after executing one instruction, the print command. The OS simply kills the console window again as no more output will be coming from the program, confusing the heck out of most novice programmers.

If you want the console window to stay alive after executing your python script:
Open up a console window, this is probably the cmd application. Navigate to the directory where the python script is located using the 'cd' command. Then execute the script using the python command.
Example:
A python script called main.py is located in c://python/project

Code: [Select]
cd python/project
python main.py
Now that the program is launched from a shell, the output should be directed the cmd terminal instead and stay alive after the script terminates.


Please note that I have not used a windows PC for the past year or so, so I am a little fuzzy on the details.
Logged
Merchants Quest me programming a trading game with roguelike elements.

FearfulJesuit

  • Bay Watcher
  • True neoliberalism has never been tried
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5056 on: September 28, 2013, 02:12:14 pm »

I tried that, but it told me it didn't recognize 'python' as a command, even though I've installed it.
Logged


@Footjob, you can microwave most grains I've tried pretty easily through the microwave, even if they aren't packaged for it.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5057 on: September 28, 2013, 02:18:46 pm »

Look in your C:\ folder, it has a python33 folder or something like that. Then use this command in the command line:
Code: [Select]
set path=%path%;C:\python33
You should now be able to run the "python" command.
Logged

FearfulJesuit

  • Bay Watcher
  • True neoliberalism has never been tried
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5058 on: September 28, 2013, 02:26:03 pm »

Ah, it looks like I had an incompatible executor for my version of Python. I've downloaded an earlier version of Python (2.7). I've still got the "command prompt closes immediately" problem...I'm sure there's a command to keep it open.
« Last Edit: September 28, 2013, 02:32:46 pm by FearfulJesuit »
Logged


@Footjob, you can microwave most grains I've tried pretty easily through the microwave, even if they aren't packaged for it.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5059 on: September 28, 2013, 02:52:31 pm »

Ah, it looks like I had an incompatible executor for my version of Python. I've downloaded an earlier version of Python (2.7). I've still got the "command prompt closes immediately" problem...I'm sure there's a command to keep it open.
You can put this line at the end of your program to make it stop shutting down immediately:
Code: [Select]
raw_input('Press ENTER to exit')But that code only works if your program doesn't have any errors, so when you have errors, you still need the console.

Open a command prompt and enter these lines:

Code: [Select]
set path=%path%;C:\python27
cd path\to\your\program\file\here

Then you can do this to run your program:
Code: [Select]
python yourfile.py

Don't close the window, otherwise you have to repeat the previous steps again.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5060 on: September 28, 2013, 03:00:58 pm »

Quick n' dirty way of doing it for notepad++ is hit F5, select the python file you want to run, then add "cmd /k " in front of the run command.

Less quick, more dirty: Find the key "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command" in regedit, edit the command to read
Code: [Select]
"C:\Windows\py.exe" "-i" "%1" %*instead of
Code: [Select]
"C:\Windows\py.exe" "%1" %*
This will force the interpreter to always open in interactive mode.
« Last Edit: September 28, 2013, 03:09:04 pm by olemars »
Logged

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5061 on: September 28, 2013, 04:58:59 pm »

So, I've been tasked with refactoring a class (in Java). The javadoc tells me that the class is a server and a thread run by said server. Secondly, looking through the otherwise well-written code, that thread has three separate functionalities which execute depending on a set of three booleans. So basically, you have a main class and three "thread-extenders" jam-packed into a single class. :(
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5062 on: September 28, 2013, 08:29:31 pm »

I fail to see the problem o_O
Too much functionality in one class?
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

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5063 on: September 28, 2013, 08:48:44 pm »

Presumably the functionality is only very vaguely related and would be better handled as several classes.  This is especially true if the switches that turn the behavior of each piece of behavior are supposed to be mutually exclusive.
Logged
Through pain, I find wisdom.

FearfulJesuit

  • Bay Watcher
  • True neoliberalism has never been tried
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5064 on: September 28, 2013, 09:42:45 pm »

I've hit a stumbling block in my CodeAcademy course, with Python dictionaries. Specifically I need to take these two dictionaries:

Code: [Select]
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
   
stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

and then multiply each respective number in the price dictionary by the respective one in the stock dictionary, and print it. The problem is, I don't really know where to line them up. I tried:

Code: [Select]
for cost in prices:
    for fruit in stock:
        print stock[fruit]*prices[cost]

but this seems to multiply everything by everything. I'm not quite sure where to go with this.

(I get mildly confused by dictionaries, incidentally, mostly confusing the actual entry with its key. I think I got it right here, though.)

[EDIT: In a flash of insight, I decided to go around the problem and instead try:

Code: [Select]
index=["banana", "apple", "orange", "pear"]

for fruit in index:
        print stock[fruit]*prices[fruit]

This works, and were I writing my own application it's what I'd do, but CodeAcademy is telling me I shouldn't have to write out any of the names of the fruit.

EDIT to the edit: Ninja'd by McFry...but that doesn't work either.

Edit to the edit to the edit: McFry's code works, except that the resulting numbers are scrambled- it gives me the numbers in descending order, not in the order of the dictionaries.
« Last Edit: September 28, 2013, 10:08:17 pm by FearfulJesuit »
Logged


@Footjob, you can microwave most grains I've tried pretty easily through the microwave, even if they aren't packaged for it.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5065 on: September 28, 2013, 09:44:06 pm »

Code: [Select]
for fruit in stock:
    print stock[fruit]*prices[fruit]
Logged

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5066 on: September 28, 2013, 11:49:37 pm »

Presumably the functionality is only very vaguely related and would be better handled as several classes.  This is especially true if the switches that turn the behavior of each piece of behavior are supposed to be mutually exclusive.
This. They all check certain things, but how and what they check are pretty different.
Logged

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5067 on: September 29, 2013, 03:09:13 am »

You could output the results to a list and then reverse said list. I don't think dictionaries care about what order you enter things into them, either. They arrange themselves, IIRC.
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.

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5068 on: September 29, 2013, 01:27:39 pm »

Code: [Select]
// Base command class
class Command {
public:
void execute(vector<string> & tokens, map<string, int> & data) = 0;
}

// Example command
class ForgeCommand: public Command {
public:
void execute(vector<string> & tokens, map<string, int> & data) {
if (tokens[2] == "iron")
{
if (tokens[3] == "armor")
{
data["Iron Bars:"] = data["Iron Bars:"] - (y * 5);
data["Iron Armor:"] = data["Iron Armor:"] + y;
}
if (tokens[nX + 3] == "weapons")
{
data["Iron Bars:"] = data["Iron Bars:"] - (y * 3);
data["Iron Weapons:"] = data["Iron Weapons:"] + y;
}
}
}
}

class CommandController {
public:
static void addCommand(string verb, Command * command) {
commands[verb] = command;
}
static void runCommand (vector<string> & tokens, map<string, int> & data) {
if (commands.find(tokens[0]) != commands.end()) {
commands[tokens[0]]->execute(tokens, data);
} else {
// Invalid token, do something about it here
}
}
private:
static map<string, Command*> commands;
}

Shouldn't I just use "map<string, int>" (with data as the parameter) instead of "map<string, int> & data" since I actually do want to change data in the functions, not just use it, or am I misunderstanding what & does in this case?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5069 on: September 29, 2013, 02:08:09 pm »

& is pass-by-reference, which means that the data in execute() IS the data from your Empire, not just a copy. This allows you to modify the data from inside execute().
Logged
Pages: 1 ... 336 337 [338] 339 340 ... 796