Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2006, 4:13 AM   #1
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Question I really should know how to do this...

I really should know how to do this, but I had a break from programming for a while and got addicted to several MMORPGs and forgot a lot of stuff.

But anyway, I was writing a simple program in a few languages to compare them and when I came to Python I thought it would be really easy, but I just couldn't do it

The program should just exit and return the value of the number of arguments that the program recieved at runtime.

Heres what I did in C++ and ASM:
--C++--
//returns the number of arguments that the program recieved at runtime.
int main(int argc, char* argv[])
{
	return argc-1;
}
--ASM--
;returns the number of arguments that the program recieved at runtime.
section .data

section .text
	global _start

	_start:
		pop ebx 
		dec ebx 
		mov eax,1
		int 80h

So when I looked at Python, I knew that the number of arguments was stored in sys.argc. So I have to return sys.argc.

Thats where I got stuck, (yes this is really simple but I completely forgot how to do it) I need to know how to return the value as an exit code like in the previous examples.

It can be tested by running the program with arguments, then running "echo $?".

Thanks for you help.

~Cold
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Apr 17th, 2006, 4:24 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Use sys.exit and len(sys.argv):
import sys
sys.exit(len(sys.argv) - 1)
Arevos is offline   Reply With Quote
Old Apr 17th, 2006, 4:43 AM   #3
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Ah thanks thats what I needed. Why do you need to call the len() method on sys.argc? I thought it held an integer value.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Apr 17th, 2006, 6:34 AM   #4
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
There is no sys.argc. Just sys.argv, which is an array of arguments passed to the application. Calling len on the argument list will give you the number of arguments.
Cerulean is offline   Reply With Quote
Old Apr 17th, 2006, 7:33 AM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
If you ever get stuck on something like that, and you know what module it's in try running help(sys) and looking for the description of what you're trying to do.
Sane is offline   Reply With Quote
Old Apr 17th, 2006, 7:46 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
C has argv and argc because C arrays don't contain information about their length. Python lists do, so the argc variable is superfluous.
Arevos is offline   Reply With Quote
Old Apr 17th, 2006, 8:09 AM   #7
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Ok I see, I was overlooking what you wrote in the code and didnt read it properly, I thought it said argc
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Apr 17th, 2006, 9:02 AM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,835
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Arevos, do you know why your site won't work for me? I can view the source and see everything that should be there, but nothing will display in the browser... I've tried waiting and reloading several times.
Sane is offline   Reply With Quote
Old Apr 17th, 2006, 11:05 AM   #9
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Quote:
Originally Posted by Sane
Arevos, do you know why your site won't work for me? I can view the source and see everything that should be there, but nothing will display in the browser... I've tried waiting and reloading several times.
This domain name expired on Mar 23, 2006.

__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Apr 17th, 2006, 3:07 PM   #10
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Sane
Arevos, do you know why your site won't work for me? I can view the source and see everything that should be there, but nothing will display in the browser... I've tried waiting and reloading several times.
Curious. Could you tell me what browser and OS you're using? Also, have you tried forcing a refresh of all images, styles and scripts with "ctrl-refresh"? I'd really appreciate any information you could give me.
Quote:
Originally Posted by coldDeath
This domain name expired on Mar 23, 2006.

What on earth are you talking about? A quick whois confirms that the renewal date for monkeyengines.co.uk is 14-Oct-2006.
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC