-1

I have multiple IPAddresses, ranging from xxx.xx.xxx.11 to xxx.xx.xxx.50. For IPAddress .11, I want to assign the string "A01" to the variable cabine. to IPAddress .12, the string "A02" etc up to .30 ("A20")

Then, from IPAddress .31, I want to asisgn the string "B01", .32 "B02" etc all the way to .50 "B20".

Although I am an absolute beginner, I thought this cannot be that hard but it just doesn't want to work!

I thought of programming it with a for loop, something like this:

for (i = 11; i < 51; i++) {
  cabine = A and something with i;
}

But I would need two different loops because of the different letters (A, B) right? Thanks in advance for any help!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Anonymosaurus
  • 99
  • 1
  • 9
  • That's not even the syntax for Python loops, that looks like C. Python uses `for i in range(11, 51):` – Barmar Nov 14 '19 at 15:51
  • Sorry, I mixed that up. I want to program that in python. I mean I already have a solution via if loops but thats not efficient at all – Anonymosaurus Nov 14 '19 at 15:52

3 Answers3

1

Use an if statement to determine the prefix A or B and how the loop index translates to the suffix number. Then use a formatting operator to add leading zeroes (see Best way to format integer as string with leading zeros?)

for i in range(11, 51):
    if i <= 30:
        prefix = 'A'
        num = i - 10
    else:
        prefix = 'B'
        num = i - 30
    cabine = f'{prefix}{num:02d}'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Sounds reasonable but how do I put together the IP address and i ? Because I get the IPAddress through a programm. It stores the IP Address in another variable called IPAddress. I don't really know how the last two digits could be converted to this i variable – Anonymosaurus Nov 14 '19 at 16:02
  • `IPAddress = f'xxx.xxx.xxx.{i}'` – Barmar Nov 14 '19 at 16:08
1

Here is a sample exemple of what you want:

cabine = []

data = ["xxx.xx.xxx.11" , "xxx.xx.xxx.24", "xxx.xx.xxx.34", "xxx.xx.xxx.45", "xxx.xx.xxx.49", "xxx.xx.xxx.50"]

for val in data:
  number = int(val[-2:])
  if(number > 10 and number<=30):
    cabine.append("A{:02d}".format(number - 10))
  elif(number > 30 and number<=50):
    cabine.append("B{:02d}".format(number - 30))

print(cabine)
Clément
  • 1,128
  • 7
  • 21
  • That looks quite well! But my IPAddresses are stored in another variable called IPAddress. I get it with some python code – Anonymosaurus Nov 14 '19 at 16:03
  • Yes, you should edit your question to make it understandable. I made there a "sample" that you can implement in you code. – Clément Nov 14 '19 at 16:12
1

For processing your IP addresses, you can use the ipaddress module. The .packed member can access each number of the IPV4Address. Then you need a formula for converting the IP address to A/B and the number you want.

from ipaddress import IPv4Address

def gen_name(ip):
    i = IPv4Address(ip).packed[3]
    return f"{'A' if i < 31 else 'B'}{(i-11)%20+1:02}"

for i in range(11,51):
    ip = f'192.168.1.{i}' # generate IPs for testing
    name = gen_name(ip)
    print(ip,name)

Output:

192.168.1.11 A01
192.168.1.12 A02
192.168.1.13 A03
192.168.1.14 A04
192.168.1.15 A05
192.168.1.16 A06
192.168.1.17 A07
192.168.1.18 A08
192.168.1.19 A09
192.168.1.20 A10
192.168.1.21 A11
192.168.1.22 A12
192.168.1.23 A13
192.168.1.24 A14
192.168.1.25 A15
192.168.1.26 A16
192.168.1.27 A17
192.168.1.28 A18
192.168.1.29 A19
192.168.1.30 A20
192.168.1.31 B01
192.168.1.32 B02
192.168.1.33 B03
192.168.1.34 B04
192.168.1.35 B05
192.168.1.36 B06
192.168.1.37 B07
192.168.1.38 B08
192.168.1.39 B09
192.168.1.40 B10
192.168.1.41 B11
192.168.1.42 B12
192.168.1.43 B13
192.168.1.44 B14
192.168.1.45 B15
192.168.1.46 B16
192.168.1.47 B17
192.168.1.48 B18
192.168.1.49 B19
192.168.1.50 B20
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251