Skip to main content
added 220 characters in body; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Right so I was doing a quick project, and sought a quick way to finish one section of my Python code. I handed it in, but I'm well aware that it's bad code., but I was wondering if there was a good way to condense this code into fewer lines.

def nGen_army(level, strength, weakness):
    if weakness == 1:
        if level == 0:
            army = round(random.randint(1000,5000),4)
            return army
        if level == 1:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 2:
            army = round(random.randint(50000,75000),4)
            return army
        if level == 3:
            army = round(random.randint(100000,200000),4)
            return army
        if level == 4:
            army = round(random.randint(200000,500000),4)
            return army
    if strength == 1:
        if level == 0:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 1:
            army = round(random.randint(50000,100000),4)
            return army
        if level == 2:
            army = round(random.randint(100000,300000),4)
            return army
        if level == 3:
            army = round(random.randint(300000,800000),4)
            return army
        if level == 4:
            army = round(random.randint(800000,1100000),4)
            return army
    else:
        if level == 0:
            army = round(random.randint(5000,10000),4)
            return army
        if level == 1:
            army = round(random.randint(25000,50000),4)
            return army
        if level == 2:
            army = round(random.randint(75000,150000),4)
            return army
        if level == 3:
            army = round(random.randint(150000,250000),4)
            return army
        if level == 4:
            army = round(random.randint(300000,900000),4)
            return army
        

I was doing a quick project and sought a quick way to finish one section of my Python code. I handed it in, but I'm well aware that it's bad code. I was wondering if there was a good way to condense this code into fewer lines.

def nGen_army(level, strength, weakness):
if weakness == 1:
    if level == 0:
        army = round(random.randint(1000,5000),4)
        return army
    if level == 1:
        army = round(random.randint(10000,25000),4)
        return army
    if level == 2:
        army = round(random.randint(50000,75000),4)
        return army
    if level == 3:
        army = round(random.randint(100000,200000),4)
        return army
    if level == 4:
        army = round(random.randint(200000,500000),4)
        return army
if strength == 1:
    if level == 0:
        army = round(random.randint(10000,25000),4)
        return army
    if level == 1:
        army = round(random.randint(50000,100000),4)
        return army
    if level == 2:
        army = round(random.randint(100000,300000),4)
        return army
    if level == 3:
        army = round(random.randint(300000,800000),4)
        return army
    if level == 4:
        army = round(random.randint(800000,1100000),4)
        return army
else:
    if level == 0:
        army = round(random.randint(5000,10000),4)
        return army
    if level == 1:
        army = round(random.randint(25000,50000),4)
        return army
    if level == 2:
        army = round(random.randint(75000,150000),4)
        return army
    if level == 3:
        army = round(random.randint(150000,250000),4)
        return army
    if level == 4:
        army = round(random.randint(300000,900000),4)
        return army

Right so I was doing a quick project, and sought a quick way to finish one section of my Python code. I handed it in, but I'm well aware that it's bad code, but I was wondering if there was a good way to condense this code into fewer lines.

def nGen_army(level, strength, weakness):
    if weakness == 1:
        if level == 0:
            army = round(random.randint(1000,5000),4)
            return army
        if level == 1:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 2:
            army = round(random.randint(50000,75000),4)
            return army
        if level == 3:
            army = round(random.randint(100000,200000),4)
            return army
        if level == 4:
            army = round(random.randint(200000,500000),4)
            return army
    if strength == 1:
        if level == 0:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 1:
            army = round(random.randint(50000,100000),4)
            return army
        if level == 2:
            army = round(random.randint(100000,300000),4)
            return army
        if level == 3:
            army = round(random.randint(300000,800000),4)
            return army
        if level == 4:
            army = round(random.randint(800000,1100000),4)
            return army
    else:
        if level == 0:
            army = round(random.randint(5000,10000),4)
            return army
        if level == 1:
            army = round(random.randint(25000,50000),4)
            return army
        if level == 2:
            army = round(random.randint(75000,150000),4)
            return army
        if level == 3:
            army = round(random.randint(150000,250000),4)
            return army
        if level == 4:
            army = round(random.randint(300000,900000),4)
            return army
        
fix indentation and find a better title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Right so I was doing a quick project, and sought a quick way to finish one section of my Python code. I handed it in, but I'm well aware that it's bad code, but. I was wondering if there was a good way to condense this code into fewer lines.

def nGen_army(level, strength, weakness):
    if weakness == 1:
        if level == 0:
            army = round(random.randint(1000,5000),4)
            return army
        if level == 1:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 2:
            army = round(random.randint(50000,75000),4)
            return army
        if level == 3:
            army = round(random.randint(100000,200000),4)
            return army
        if level == 4:
            army = round(random.randint(200000,500000),4)
            return army
    if strength == 1:
        if level == 0:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 1:
            army = round(random.randint(50000,100000),4)
            return army
        if level == 2:
            army = round(random.randint(100000,300000),4)
            return army
        if level == 3:
            army = round(random.randint(300000,800000),4)
            return army
        if level == 4:
            army = round(random.randint(800000,1100000),4)
            return army
    else:
        if level == 0:
            army = round(random.randint(5000,10000),4)
            return army
        if level == 1:
            army = round(random.randint(25000,50000),4)
            return army
        if level == 2:
            army = round(random.randint(75000,150000),4)
            return army
        if level == 3:
            army = round(random.randint(150000,250000),4)
            return army
        if level == 4:
            army = round(random.randint(300000,900000),4)
            return army
        

Right so I was doing a quick project, and sought a quick way to finish one section of my Python code. I handed it in, but I'm well aware that it's bad code, but I was wondering if there was a good way to condense this code into fewer lines.

def nGen_army(level, strength, weakness):
    if weakness == 1:
        if level == 0:
            army = round(random.randint(1000,5000),4)
            return army
        if level == 1:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 2:
            army = round(random.randint(50000,75000),4)
            return army
        if level == 3:
            army = round(random.randint(100000,200000),4)
            return army
        if level == 4:
            army = round(random.randint(200000,500000),4)
            return army
    if strength == 1:
        if level == 0:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 1:
            army = round(random.randint(50000,100000),4)
            return army
        if level == 2:
            army = round(random.randint(100000,300000),4)
            return army
        if level == 3:
            army = round(random.randint(300000,800000),4)
            return army
        if level == 4:
            army = round(random.randint(800000,1100000),4)
            return army
    else:
        if level == 0:
            army = round(random.randint(5000,10000),4)
            return army
        if level == 1:
            army = round(random.randint(25000,50000),4)
            return army
        if level == 2:
            army = round(random.randint(75000,150000),4)
            return army
        if level == 3:
            army = round(random.randint(150000,250000),4)
            return army
        if level == 4:
            army = round(random.randint(300000,900000),4)
            return army
        

I was doing a quick project and sought a quick way to finish one section of my Python code. I handed it in, but I'm well aware that it's bad code. I was wondering if there was a good way to condense this code into fewer lines.

def nGen_army(level, strength, weakness):
if weakness == 1:
    if level == 0:
        army = round(random.randint(1000,5000),4)
        return army
    if level == 1:
        army = round(random.randint(10000,25000),4)
        return army
    if level == 2:
        army = round(random.randint(50000,75000),4)
        return army
    if level == 3:
        army = round(random.randint(100000,200000),4)
        return army
    if level == 4:
        army = round(random.randint(200000,500000),4)
        return army
if strength == 1:
    if level == 0:
        army = round(random.randint(10000,25000),4)
        return army
    if level == 1:
        army = round(random.randint(50000,100000),4)
        return army
    if level == 2:
        army = round(random.randint(100000,300000),4)
        return army
    if level == 3:
        army = round(random.randint(300000,800000),4)
        return army
    if level == 4:
        army = round(random.randint(800000,1100000),4)
        return army
else:
    if level == 0:
        army = round(random.randint(5000,10000),4)
        return army
    if level == 1:
        army = round(random.randint(25000,50000),4)
        return army
    if level == 2:
        army = round(random.randint(75000,150000),4)
        return army
    if level == 3:
        army = round(random.randint(150000,250000),4)
        return army
    if level == 4:
        army = round(random.randint(300000,900000),4)
        return army

Abundance of if statements Army strength based on level and modifiers

def nGen_army(level, strength, weakness):
    if weakness == 1:
        if level == 0:
            army = round(random.randint(1000,5000),4)
            return army
        if level == 1:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 2:
            army = round(random.randint(50000,75000),4)
            return army
        if level == 3:
            army = round(random.randint(100000,200000),4)
            return army
        if level == 4:
            army = round(random.randint(200000,500000),4)
            return army
    if strength == 1:
        if level == 0:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 1:
            army = round(random.randint(50000,100000),4)
            return army
        if level == 2:
            army = round(random.randint(100000,300000),4)
            return army
        if level == 3:
            army = round(random.randint(300000,800000),4)
            return army
        if level == 4:
            army = round(random.randint(800000,1100000),4)
            return army
    else:
        if level == 0:
            army = round(random.randint(5000,10000),4)
            return army
        if level == 1:
            army = round(random.randint(25000,50000),4)
            return army
        if level == 2:
            army = round(random.randint(75000,150000),4)
            return army
        if level == 3:
            army = round(random.randint(150000,250000),4)
            return army
        if level == 4:
            army = round(random.randint(300000,900000),4)
            return army
        

Abundance of if statements

def nGen_army(level, strength, weakness):
if weakness == 1:
    if level == 0:
        army = round(random.randint(1000,5000),4)
        return army
    if level == 1:
        army = round(random.randint(10000,25000),4)
        return army
    if level == 2:
        army = round(random.randint(50000,75000),4)
        return army
    if level == 3:
        army = round(random.randint(100000,200000),4)
        return army
    if level == 4:
        army = round(random.randint(200000,500000),4)
        return army
if strength == 1:
    if level == 0:
        army = round(random.randint(10000,25000),4)
        return army
    if level == 1:
        army = round(random.randint(50000,100000),4)
        return army
    if level == 2:
        army = round(random.randint(100000,300000),4)
        return army
    if level == 3:
        army = round(random.randint(300000,800000),4)
        return army
    if level == 4:
        army = round(random.randint(800000,1100000),4)
        return army
else:
    if level == 0:
        army = round(random.randint(5000,10000),4)
        return army
    if level == 1:
        army = round(random.randint(25000,50000),4)
        return army
    if level == 2:
        army = round(random.randint(75000,150000),4)
        return army
    if level == 3:
        army = round(random.randint(150000,250000),4)
        return army
    if level == 4:
        army = round(random.randint(300000,900000),4)
        return army
        

Army strength based on level and modifiers

def nGen_army(level, strength, weakness):
    if weakness == 1:
        if level == 0:
            army = round(random.randint(1000,5000),4)
            return army
        if level == 1:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 2:
            army = round(random.randint(50000,75000),4)
            return army
        if level == 3:
            army = round(random.randint(100000,200000),4)
            return army
        if level == 4:
            army = round(random.randint(200000,500000),4)
            return army
    if strength == 1:
        if level == 0:
            army = round(random.randint(10000,25000),4)
            return army
        if level == 1:
            army = round(random.randint(50000,100000),4)
            return army
        if level == 2:
            army = round(random.randint(100000,300000),4)
            return army
        if level == 3:
            army = round(random.randint(300000,800000),4)
            return army
        if level == 4:
            army = round(random.randint(800000,1100000),4)
            return army
    else:
        if level == 0:
            army = round(random.randint(5000,10000),4)
            return army
        if level == 1:
            army = round(random.randint(25000,50000),4)
            return army
        if level == 2:
            army = round(random.randint(75000,150000),4)
            return army
        if level == 3:
            army = round(random.randint(150000,250000),4)
            return army
        if level == 4:
            army = round(random.randint(300000,900000),4)
            return army
        
Fixed title and capitalisation.
Source Link
Mast
  • 13.5k
  • 12
  • 54
  • 119
Loading
Source Link
Loading