Create a username using Python

Question:

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!

Asked By: MCly

||

Answers:

When you call the input() function and assign it to first, like this:

    first = input(first)

what you’re doing is printing first (so "Darth"), waiting for the user to enter something, and then assigning that to first. That’s why your script prints Darth — it’s waiting for you to tell it what first is.

But since you already passed the first and last name via the command line, you don’t want to do that — so just remove those two input lines. Make sure to convert the len to a str before you add it to the other strings!

def full_name(first, last):
    return (first[0] + last[:7] + str(len(first+last))).lower()
Answered By: Samwise

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.

Answered By: OneCricketeer
  1. You ask to input names even if they are in sys.argv.
  2. Input("Enter name: ") accepts string that help to understand what you should input.
  3. As OneCricketeer commented, you trying to get first 7 letters instead of last.

Here is an example that accepts names from command line otherwise prompt to input.

import sys


def full_name(_first, _last):
    return f"{(_first[0] + _last[-7:])}{len(_first + _last)}"


if __name__ == '__main__':

    first = last = ""

    if len(sys.argv) == 3:
        if sys.argv[1]:
            first = sys.argv[1].lower()
        if sys.argv[2]:
            last = sys.argv[2].lower()

    if not first:
        first = input("Input first name: ").lower()
    if not last:
        last = input("Input last name: ").lower()

    username = full_name(first, last)
    print("Your username is", username)

Answered By: Ronin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.