New to Python & hello

This is the place for queries that don't fit in any of the other categories.

New to Python & hello

Postby Pointer » Thu Feb 06, 2014 5:54 pm

Hi Python folk,

I'm just dipping my toe into Python for the first time by trying to tweak some existing code and wondering if I can ask for some help?

Currently the script passes some info like this into python from the command like: script.py word1,word2,12345:678:9,12345

I'm looking to enter this info after a request from the script using something like: packet = input('Enter Packet: ');

This is my failed attempt, wondering what I'm doing wrong? Thanks for any tips.
Code: Select all
packet = input('Enter Packet: ');
for arg in sys.argv[1:]:
    last_comma = arg.rfind(',')
    if last_comma < 0:
        print("Invalid input:" + arg)
        continue
    try:
        crc1 = int(arg[last_comma + 1:])
    except:
        print("Invalid input:" + arg)
        continue
    arg = arg[:last_comma]
    crc2 = get_crc16_xmodem(arg)
    if crc1 == crc2:
        print("CRC OK: %(arg)s" % vars())
    else:
        print('CRC ERROR: "%(arg)s" expected %(crc2)d but got %(crc1)s' % vars())
Last edited by stranac on Thu Feb 06, 2014 5:59 pm, edited 1 time in total.
Reason: Added code tags. Locked.
Pointer
 
Posts: 1
Joined: Thu Feb 06, 2014 5:47 pm

Re: New to Python & hello

Postby stranac » Thu Feb 06, 2014 6:03 pm

My best guess would be that you want to get rid of the for loop , and use packet everywhere where arg was used.
But I can't be sure, since you haven't explained what you want all that well.

It looks like that code was written by someone not very familiar with python...

Also, please read this to learn how to post code, as well as other useful stuff:
viewtopic.php?f=10&t=145
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 7 guests