Search code examples
pythonsys

Create a username using Python


I'm just learning Python and have to create a program that has a function that takes as arguments a first and last name and creates a username. The username will be the first letter of the first name in lowercase, the last 7 of the last name in lowercase, and the total number of characters in the first and last names. For example, Jackie Robinson would be jobinson14. I have to use sys.argv.

This is my code:

import sys

def full_name(first, last):
    first = input(first)
    last = input(last)
    username = lower((first[0] + last[:7])) + len(first+last)
    return username
first = sys.argv[1]
last = sys.argv[2]
username = full_name(first, last)    
print ("Your username is",username)

When entering Darth Vader

Expected output:
Your username is dvader10

Actual output:
Darth

Please help!


Solution

  • Actual output: Darth

    Not exactly. You are using input(first), which is waiting for you to type something...

    Using sys.argv means you need to provide the arguments when running the code

    python app.py Darth Vader
    

    And if you remove the input() lines, this would return without prompting for input, and not show Darth


    As shown, you are trying to read from arguments and prompt for input.

    If you did want to prompt, then you need to remove the import

    def full_name(first, last):
        return (first[0] + last[-7:] + str(len(first+last))).lower()
    
    first = input('First Name: ')
    last = input('Last Name: ')
    username = full_name(first, last)    
    print("Your username is",username)
    

    And just run the script directly. But you say you have to use sys.argv, so the solution you're looking for is to not use input() at all.