Home > Net >  How to randomize the first letters of your first, and last name
How to randomize the first letters of your first, and last name

Time:10-07

My teacher has assigned us homework (ICS110P) beginner python, and in the homework, I am supposed to create code that takes the first name and last name of user input and randomizes the first letter of each name given. I've gone through all my notes looking for something that can do this, but unable to find it. I've asked my teacher, but she never helps.

"First, please enter your first name: Nikki Now, please enter your last name: Manuel

I'm going to switch the letters of your first and last name to give you a funny name! Your new name is....... Mikki Nanuel HAHA! Isn't that hilarious?"

 print('Option 3! lets switch up them letters!')
            f_name = input(str("Input your First Name: "))
            l_name = input(str("Input your Last Name: "))
            print()
            print(f_name [0:1],  l_name [0:1]) # This is just me trying to use anything. 
            print()
            print('Thanks for using my program!')

This is what I have so far, I just can't remember what to do with the print. This is part of my homework assignment that we are learning the Def function.

CodePudding user response:

This code works:

import random

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
name = input("Input your name: ")
random_letter = letters[random.randint(0, len(letters)-1)]
new_name = random_letter   name[1:len(name)]
print(new_name)

I am relatevely new to python, so surely there is a better way to do it!

CodePudding user response:

from random import *
print('Option 3! lets switch up them letters!')
f_name = input(str("Input your First Name: "))
l_name = input(str("Input your Last Name: "))

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
           'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

first_letter = randrange(1, 27)
second_letter = randrange(1, 27)

f_name[0] = letters[first_letter]
l_name[0] = letter[second_letter]

print(
    f"I'm going to switch the letters of your first and last name to give you a 
funny name! Your new name is: {f_name} {l_name}.")
print('Thanks for using my program!')

here is what you need.

CodePudding user response:

Maybe you could consider using random.choice:

from string import ascii_uppercase
from random import choice

def get_name_from_user() -> (str, str):
    """Obtains first and last names from user"""
    first_name = input("Input your first name: ")
    if not first_name:
        raise ValueError("Your first name must have atleast one character")
    last_name = input("Input your last name: ")
    if not last_name:
        raise ValueError("Your last name must have atleast one character")
    return (first_name, last_name)

def get_switched_letters_name(f_name: str, l_name: str) -> str:
    """Returns name with the first characters of the first and last names switched"""
    return f"{l_name[0]}{f_name[1:]} {f_name[0]}{l_name[1:]}"

def get_random_letters_name(f_name: str, l_name: str) -> str:
    """Returns name with the first characters of the first and last names randomized"""
    return f"{choice(ascii_uppercase)}{f_name[1:]} {choice(ascii_uppercase)}{l_name[1:]}"

def main() -> None:
    print("Welcome to my program!")
    f_name, l_name = get_name_from_user()
    print("Your name is:")
    print(f"{f_name} {l_name}")
    print("Your name with switched first letters is:")
    switched_letters_name = get_switched_letters_name(f_name, l_name)
    print(switched_letters_name)
    print("Your name with random first letters is:")
    random_letters_name = get_random_letters_name(f_name, l_name)
    print(random_letters_name)
    print('Thanks for using my program!')

if __name__ == '__main__':
    main()

Example Usage:

Welcome to my program!
Input your first name: Nikki
Input your last name: Manuel
Your name is:
Nikki Manuel
Your name with switched first letters is:
Mikki Nanuel
Your name with random first letters is:
Yikki Hanuel
Thanks for using my program!
  • Related