![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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 80hSo 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.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Use sys.exit and len(sys.argv):
import sys sys.exit(len(sys.argv) - 1) |
|
|
|
|
|
#3 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
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.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
C has argv and argc because C arrays don't contain information about their length. Python lists do, so the argc variable is superfluous.
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() |
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.
|
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Quote:
![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
|
#10 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Quote:
|
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|