-4
\$\begingroup\$

I want to make this clean and shorter and I was wondering how I can do it? While loop would be best but how do i implement it?

if line.startswith('ACME'):
        if counter == 2:
            compname, value = line.split('(')
            value = float(value[0:-14])
            products[f'{compname}'] = value
        if counter == 3:
            compname, value = line.split('(')
            value = float(value[0:-14])
            products[f'{compname}'] = value 
        if counter == 4:
            compname, value = line.split('(')
            value = float(value[0:-14])
            products[f'{compname}'] = value
        if counter == 5:
            compname, value = line.split('(')
            value = float(value[0:-14])
            products[f'{compname}'] = value 

'''

| |
\$\endgroup\$
  • 3
    \$\begingroup\$ What's the purpose of the checks? They all look the same. It looks like you could just change the condition to if 2 <= counter <= 5:, then get rid of all but one of those bodies. \$\endgroup\$ – Carcigenicate Nov 21 at 23:18
  • \$\begingroup\$ would belong to codereview site, but maybe would be downvoted there... I migrated it there, but maybe it wasn't the best choice. \$\endgroup\$ – Jean-François Fabre Nov 21 at 23:20
  • 1
    \$\begingroup\$ Ya, I don't think this should have been migrated. It's more appropriate here, but I think it lacks context to be ontopic. \$\endgroup\$ – Carcigenicate Nov 21 at 23:22
  • 1
    \$\begingroup\$ Why are you even using an if statement? The contents of each branch are identical. \$\endgroup\$ – TigerhawkT3 Nov 22 at 0:12
  • 1
    \$\begingroup\$ This question as-is does not provide the required context and is thus off-topic for Code Review. It shouldn't have been migrated. \$\endgroup\$ – Mast Nov 22 at 8:04