0

This is just a simple game I tried to put together that asks you a couple questions and then automatically formats a new passcode out of the answers.

Since I am a beginner I'd like to know of some techniques I could use that would increase the efficiency of this code, and maybe remove the crazy amount of functions I've declared. Thank You!

minimum = 3
maximum = 10

name = input("What is your name? ")

def nameInput():

    if len(name) < minimum:
        print("Name is too small")
        return nameInput()
    elif len(name) > maximum:
            print("Name is too large")
            return nameInput()

nameInput()

food = input("What is your favorite food? ")

def foodInput():

    if len(food) < minimum:
        print("Enter a food name longer than " + (food))
        return foodInput()
    elif len(food) > maximum:
            print("Enter a food name shorter than " + (food))
            return foodInput()

foodInput()

birthday = input("What is your date of birth? (mmddyyyy) ")

nameIndex = name[0:2]
foodIndex = food[2: ]
birthIndex = birthday[0:3]

passcode = nameIndex + foodIndex + birthIndex

print("Your password is " + passcode)
Jake
  • 13
  • 1
  • 1
    There are two functions. I wouldn't consider that a "crazy amount". – mkrieger1 Oct 12 '17 at 13:13
  • 1
    You could make a single function `get_input(question, min_len, max_len)` – mshsayem Oct 12 '17 at 13:16
  • 5
    If you are willing to accept feedback on all aspects of your code besides how to make it shorter, you could post it on https://codereview.stackexchange.com/. – mkrieger1 Oct 12 '17 at 13:16
  • Did you notice that you will enter into an infinite loop without taking further inputs if any of first two input fails for either of condition i.e less than minimum or more than maximum? – Prasad Oct 12 '17 at 13:18
  • What loop? There's no loop at all in this code. – Błotosmętek Oct 12 '17 at 13:21
  • Hey guys, where is the fascination with recursion coming from? It seems to me that someone is teaching students to use recursion where loops would be a better fit. – quamrana Oct 12 '17 at 13:21
  • And also, I would like to add parameters for the date of birth input, but I keep getting recursion depth errors. – Jake Oct 12 '17 at 13:23
  • To avoid the infinite loop, put the name variable inside the function. This way, every time it is called, the user will be able to insert a name according to the instructions and if he manages to do the right thing, the code execution will proceed. – raratiru Oct 12 '17 at 13:25
  • @raratiru Awesome, thanks. – Jake Oct 12 '17 at 13:31
  • @user1190882 Oh, I see now. Yes, this is purposeful in order that no one can get past either step until they properly answer the question. – Jake Oct 12 '17 at 13:35

1 Answers1

1

If you're after brevity, try to use a single function multiple times rather than multiple functions a single time

def get_input(variable_name, min_size, max_size, begin, end):                 
    data = input("What is your {}? ".format(variable_name))                   
    if len(data) < min_size:                                                  
        raise ValueError('{} is too small'.format(variable_name.capitalize()))
    elif len(data) > max_size:                                                
        raise ValueError('{} is too big'.format(variable_name.capitalize()))  
    return data[begin:end]                                                    

name_index = get_input("name", 3, 10, 0, 2)                                   
food_index = get_input("food", 3, 10, 2, 100)                                 
birth_index = get_input("date of birth (mmddyyyy)", 3, 10, 0, 3)              
passcode = name_index + food_index + birth_index                              

print("Your passcode is", passcode)                                                                  
triphook
  • 2,915
  • 3
  • 25
  • 34