Samuel Cobb
Samuel Cobb

Reputation: 1

Is there anyway to simplify if/elif blocks?

im a pretty new (and bad) at python, and I was just wondering if there is anyone who can help me simplify a if/elif block. It's a super basic code, which basically asks what someone wants in a password and then randomises a pswd for them. But there are options if the user wants uppercase and numbers.

if upper == True and numbers == True:
    lettersAndDigits = string.ascii_letters + string.digits
elif upper == True and numbers == False:
    lettersAndDigits = string.ascii_letters
elif upper == False and numbers == True:
    lettersAndDigits = string.ascii_lowercase + string.digits
elif upper == False and numbers == False:
    lettersAndDigits = string.ascii_lowercase

sorry for the noob question guys

thanks a bunch guys, this has really helped :)

just reviewing for my igcse's and Im trying to relearn proper procedure. my teacher isn't that strong in python

Upvotes: 0

Views: 123

Answers (7)

Maciej Nachtygal
Maciej Nachtygal

Reputation: 59

I'm not sure what types are your upper and numbers variables, but unless their values are None, 0, [] (empty list), {} (empty dict), '' (empty string) they all are True. So you can simplify it to:

if variable:        # if variable == True:
    pass

or

if not variable:    # if variable == False: 
    pass

So simplified:

if upper:
    letters_and_digits = string.ascii_letters
else:
    letters_and_digits = string.ascii_lowercase
if numbers:
    letters_and_digits += string.digits

Also, look here to read about proper way of naming your variables.

Upvotes: 0

Simon Brahan
Simon Brahan

Reputation: 2076

You could start your character list in an empty array, and add characters to is as you go. Something like:

lettersAndDigits = []
if upper:
    lettersAndDigits += string.ascii_uppercase

if lower:
    lettersAndDigits += string.ascii_lowercase

if numbers:
    lettersAndDigits += string.digits

This will also allow you to use any combination, whereas you only account for four combinations in your code.

Upvotes: 3

amunozo
amunozo

Reputation: 67

I don't think you can simplify that much more. Nevertheless, you can express it differently nested if blocks that maybe is easier to read:

if upper == True:
    if numbers == True:
        lettersAndDigits = string.ascii_letters + string.digits
    else:
        lettersAndDigits = string.ascii_letters
else:
    if numbers == True:
        lettersAndDigits = string.ascii_lowercase + string.digits
    else:
        lettersAndDigits = string.ascii_lowercase

Upvotes: 1

hilberts_drinking_problem
hilberts_drinking_problem

Reputation: 11602

Here is an abusive one-liner (don't use this!):

allowed_chars = [string.ascii_lowercase, string.ascii_letters][upper] + string.digits * numbers

Using conversion of bool to 0, 1 to index a list and str by int multiplication, followed by str addition.

Upvotes: 0

Jongware
Jongware

Reputation: 22478

Create a small list with the 4 combinations, then use upper and numbers to select the correct string right away. No ifs required.

import string

lettersAndDigits = [[string.ascii_lowercase,string.ascii_lowercase + string.digits],
    [string.ascii_letters,string.ascii_letters + string.digits]]

upper = False
numbers = True
print (lettersAndDigits[upper][numbers])
# abcdefghijklmnopqrstuvwxyz0123456789
upper = True
numbers = False
print (lettersAndDigits[upper][numbers])
# abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Upvotes: 1

Amadan
Amadan

Reputation: 198436

Every == True is useless. After that, notice that numbers have something in common, and extract it out.

if upper:
    lettersAndDigits = string.ascii_letters
else:
    lettersAndDigits = string.ascii_lowercase

if numbers:
    lettersAndDigits += string.digits

You could squeeze it a bit further, but Pythonistas will probably hate it on principle:

lettersAndDigits = string.ascii_letters if upper else string.ascii_lowercase
if numbers:
    lettersAndDigits += string.digits

Upvotes: 5

damaredayo
damaredayo

Reputation: 1079

So one thing you can do straight away is get rid of the == True and == False part, as the variables themselves are booleans which you are just comparing to see if they are booleans. As well, you don't need to do elif becuase only one of these wll return true, so you can just have a bunch of if statements which does look a bit nicer.

if upper and numbers:
    lettersAndDigits = string.ascii_letters + string.digits

if upper and not numbers:
    lettersAndDigits = string.ascii_letters

if not upper and numbers:
    lettersAndDigits = string.ascii_lowercase + string.digits

if not upper and not numbers
    lettersAndDigits = string.ascii_lowercase

Other than that, in this example there's not a whole lot more you can do. Programming unfortunately sometimes is pretty ugly, and python doesn't have switch so you're out of luck with that.

If anyone has any other thoughts I hadn't considered feel free to correct me.

Upvotes: 1

Related Questions