We included lots of challenges throughout Captain Code, sometimes multiple per chapter. If you need some help, you’ll find solutions to the challenges are presented here. Click on any challenge below to expand it.

Just keep in mind that there is never one solution to any coding challenge, so if your solutions look different but produce the desired result, that’s ok.

Challenge 2.1

So, here is your first Challenge. Hello4.py created two variables, firstName and lastName, and then combined them into a new variable, named fullName. Modify that code so that it asks the user for a first name and last name instead of using the hard-coded values.

Here’s a hint: You only need to change the first two lines of code so that each line uses an input() function. Can you figure this one out?

firstName = input("What is your first name? ")
lastName = input("What is your last name? ")
fullName = firstName + " " + lastName

print("Hello, my name is", fullName, "and I'm a coder!")

Challenge 2.2

Make your Mad Lib interesting by prompting for at least 15 different words. And then personalize it. At the start, when you provide instructions, ask the user for their name, and then use that in the instructions to create a more personalized experience.

print("Hello, please answer the following prompts.")
print()

animal = input("Enter an animal: ")
name = input("Enter a name: ")
adjective1 = input("Enter an adjective: ")
color1 = input("Enter a color: ")
adjective2 = input("Enter an adjective: ")
noun1 = input("Enter a noun: ")
noun2 = input("Enter a noun: ")
noun3 = input("Enter a noun: ")
adjective3 = input("Enter an adjective: ")
color2 = input("Enter a color: ")
noun4 = input("Enter a noun: ")
verb = input("Enter a verb: ")
adjective4 = input("Enter an adjective: ")
color3 = input("Enter a color: ")
animal2 = input("Enter an animal: ")

print("Thank you. Here is your story.")
print()
print("I have a pet", animal, "named", name, ".")
print("He is", adjective1, ",", color1, ", and", adjective2, ".")
print(name, "eats", noun1, ",", noun2, ", and", noun3, ".")
print("His favorite toy is a", adjective3, color2, noun4, ".")
print("He likes to", verb, "with his friend the", color3, adjective4, animal2)

Challenge 3.1

Ok, this one is a little harder, but you can do it, promise! See that list with the five animals? Write code that creates two lists, one with animals, like this:

animals=["ant","bat","cat","dog","eel"]

You can use your own list of animals, and you can have more than five (the more the better).

Then create a similar list of adjectives, things like big, green, smelly, cute, and so on. (Again, the more, the better. And it doesn’t matter whether or not your two lists have the same number of items.)

Then pick a random adjective and a random animal and save each to a variable. (You’ll need two variables: one for your animal and one for your adjective.) Then print() the choices so that the output is something like I have a cute eel. Each time you run the app, you’ll get a different combination.

import random

animals=["ant","bat","cat","dog","eel"]
adjectives=["big","small","cute","smelly","shiny"]

choiceAnimal = random.choice(animals)
choiceAdjective = random.choice(adjectives)

print("I have a", choiceAdjective, choiceAnimal)

Challenge 3.2

Most dice we use have 6 sides, but some games use dice with more sides. And, actually, Ancient Greeks and Romans used dodecahedron shaped dice, which have 12 sides! So, just in case you ever run into an ancient Greek or Roman, write code that rolls a 12-sided die.

# Imports
import random

# Roll the die
die = random.randrange(1, 13)
 
# Display value of dodecahedron die!
print("You rolled a", die, "!")

Challenge 4.1

Modify Date2.py to also display the current time. The properties you want are called hour and minute.

# Imports
import datetime

# Get today's date
today = datetime.datetime.now()

# Print today's year, month, day, hour, and minute
print("The year is", today.year)
print("The month is", today.month)
print("The day is", today.day)
print("The hour is", today.hour)
print("The minute is", today.minute)

Challenge 5.1

You wouldn’t want someone else with the same name getting the same advantage. How could you modify this code to make it a little more private? You could require the name to be typed a specific way (all lowercase maybe), or have a space or two at the end. Come up with an option and modify the if statement to check for it.

# Imports
import random

# Ask the user for their name
name=input("What is your name?: ")
print("Hello", name, "let's play Rock, Paper, or Scissors")

# Computer picks one
cChoice = random.choice("RPS")
 
# Get user choice
print("Rock, Paper, or Scissors?")
uChoice=input("Enter R, P, S: ").upper().strip()

# TOP SECRET CODE - Modified for secrecy! 
# ('$' and space before and after)
if name == " $hmuel ":
    if uChoice == "R":
        cChoice = "S"
    elif uChoice == "P":
        cChoice = "R"
    elif uChoice == "S":
        cChoice = "P"

# Compare choices
if cChoice == uChoice:
    print("It's a tie!")
elif uChoice == "R" and cChoice == "P":
    print("You picked rock, computer picked paper. You lose.")
elif uChoice == "P" and cChoice == "R":
    print("You picked paper, computer picked rock. You win.")
elif uChoice == "R" and cChoice == "S":
    print("You picked rock, computer picked scissors. You win.")
elif uChoice == "S" and cChoice == "R":
    print("You picked scissors, computer picked rock. You lose.")
elif uChoice == "P" and cChoice == "S":
    print("You picked paper, computer picked scissors. You lose.")
elif uChoice == "S" and cChoice == "P":
    print("You picked scissors, computer picked paper. You win.")
else:
    print("Not very good at listening to instructions. Huh?")

Challenge 6.1

range() takes an optional third argument—a step. If you specify range(1, 11, 2), the loop counter will increase by 2 each time, so the loop will run 5 times instead of 10 (for 1, 3, 5, 7, and 9). Try to create a loop that displays the numbers 10, 20, 30, all the way to 100.

# Loop from 10 to 100, counting by 10
for i in range(10, 101, 10):
    # Display i in each loop iteration
    print(i)

Challenge 6.2

As you have seen, Encrypt.py and Decrypt.py are almost identical. In truth, they should have been the same program. We just separated them to make the code a little simpler.

action = input("Encrypt or decrypt? Enter E or D: ")

Then, in your code, you can use if statements to select the encrypt or decrypt versions of the code, based on action being E or D.

# ASCII range of usable characters - anything out of this range could throw errors
asciiMin = 32   # Represents the space character - " "
asciiMax = 126  # Represents the tilde character - "~"
 
# Secret key
key = 314159    # Top secret! This is the encryption key!
key = str(key)  # Convert to string so can access individual digits

# Determine if encrypting or decrypting
action = input("Encrypt or decrypt? Enter E or D: ")

# Get input message (either encrypted message to decrpyt or message to be encrypted)
if action == 'E':
    message = input("Enter message to be encrypted: ")
elif action == 'D':
    message = input("Enter message to be decrypted: ")
 
# Initialize variable for encrypted message
messEncr = ""

# Only run loop if entered E or D
if action in ['D', 'E']: 
    # Loop through message
    for index in range(0, len(message)):
        # Get the ASCII value for this character
        char = ord(message[index])
        # Is this character out of range?
        if char < asciiMin or char > asciiMax:
            # Yes, not safe to encrypt, leave as is
            messEncr += message[index]
        else:
            # Safe to encrypt or decrypt this character
            if action == 'E':
                # Encrypt and shift the value as per the key
                ascNum = ord(message[index]) + int(key[index % len(key)])
                # If shifted past range, cycle back to the beginning of the range
                if ascNum > asciiMax:
                    ascNum -= (asciiMax - asciiMin)
            elif action == 'D':
                # Decrypt and shift the value as per the key
                ascNum = ord(message[index]) - int(key[index % len(key)])
                # If shifted past range, cycle back to the end of the range
                if ascNum < asciiMin:
                    ascNum += (asciiMax - asciiMin)
            # Convert to a character and add to output
            messEncr = messEncr + chr(ascNum)
    
    # Display result
    print("Encrypted message:", messEncr)
else:
    print("You must ener E or D only.")

Challenge 7.1

Double challenge for you this time.

First, look at the final print() statement. It displays the sorted list, but the output doesn’t look that good. So change that output to use a for loop printing the sorted animals one per line.

Second, make sure the user doesn’t type an animal already in the list. How? Refer back to Chapter 6 if you need a reminder of how to check if an item is in a list. Then modify the if statement so that in addition to checking for the length of the input, it also checks to ensure that the item is not already in the list. Your condition will have two parts, and you’ll want to use and to join them.

# Create the empty animals array
animals = []
 
# Variable for input
userInput = " "
 
# Give instructions to user
print("I can sort animals for you.")
print("Enter your animals, one at a time.")
print("When you are done just press Enter.")
 
# Loop until get an empty string
while userInput != "":
    # Get input
    userInput=input("Enter an animal, leave empty to end: ").strip()
    # Make sure it is not empty or duplicate
    # Note, the 'not' says "if userInput" isn't in the animals array"
    # See later in Chapter 7 for more information
    if len(userInput) > 0 and userInput not in animals:
        # It's not empty, add it
        animals.append(userInput)
 
# Sort data
animals.sort()
 
# Display the list - formatted 
for animal in animals:
    print(animal)

Challenge 7.2

This one is tricky, but you can do it. Can you provide more feedback? Instead of always displaying Too low or Too high, can you display Too low or Too high if they are close and Much too high and Much too low if they are way off? Think about it.

# Guess the number between a specified range.
# User is told if the number guess is too high or too low.
# Game tells the user how many guesses were needed
 
# Imports
import random
 
# Define variables
guesses = 0     # To keep track of how many guesses
numMin = 1      # Start of number range
numMax = 100    # End of number range
userInput = ""  # This holds the user's input
userGuess = 0   # This holds the user's input as a number
farOff = 15     # This is how far off will be considered way off
 
# Generate random number
randNum = random.randrange(numMin, numMax+1)
 
# Instructions for user
print("I am thinking of a number between", numMin, "and", numMax)
print("Can you guess the number?")
 
# Loop until the user has guessed it
while randNum != userGuess:
    # Get user guess
    userInput=input("Your guess: ").strip()
    # Make sure the user typed a valid number
    if not userInput.isnumeric():
        # Input was not a number
        print(userInput, "is not a valid number!")
    else:
        # Input was a number, good to proceed
        # Increment guess counter
        guesses=guesses+1
        # Convert the input text to a number
        userGuess=int(userInput)
        # Check the number
        if userGuess < numMin or userGuess > numMax:
            print(userGuess, "is not between", numMin, "and", numMax)
        elif userGuess + farOff < randNum:
            print("Way too low. Try again.")
        elif userGuess < randNum:
            print("Too low. Try again.")
        elif userGuess - farOff > randNum:
            print("Way too high. Try again.")
        elif userGuess > randNum:
            print("Too high. Try again.")
        else:
            print("You got it in", guesses, "tries")
 
# Goodbye message
print("Thanks for playing!")

Challenge 9.1

On second thought, we handled the user input badly in this code. Why? If the user entered too many characters, we opted to use the first of them and ignore the rest. That works. But what if the user enters no characters at all? That’s a situation we didn’t plan for! Oops!

No worries, that’s why coders write version 2 (or version 1.1, you get the idea) of their apps. So, update the code so that it catches all invalid input lengths (too long or too short). You can do this with a while loop, like this:

currGuess = ""
while len(currGuess) != 1:
# Imports
import random
 
# Variables
maxLives = 7        # Maximum tried
maskChar = "_"      # Mask character
livesUsed = 0       # Try counter
guessedLetters = [] # To store guesses
wordLetters = []    # All of the letters in the word
victory = False     # Will be True if word is guessed
 
#Game words
gameWords = ["anvil", "boutique", "cookie", "fluff",
            "jazz", "pneumonia", "sleigh", "society",
            "topaz", "tsunami", "yummy", "zombie",
            "rhythm"]
 
# Pick the word for the game
gameWord = random.choice(gameWords)
 
# Get all the letters in this word
for letter in gameWord:
    # Make sure we don't have this one in the list
    letterInList = False
    # Loop through the letters
    for wl in wordLetters:
        # Check each one
        if wl == letter:
            # We have this one
            letterInList = True
    # If we don't have it yet
    if not letterInList:
        # Add it to the list
        wordLetters.append(letter)
# Now sort the list
wordLetters.sort()
 
# Actual game starts here
# Loop until out of tries or guessed word correctly
while livesUsed < maxLives and victory == False:
 
    # First we need to mask the word
    # Start with an empty string
    displayWord = ""
    # Loop through word
    for letter in gameWord:
         # Has this letter been guessed?
         if letter in guessedLetters:
             # This one has been guessed so add it
             displayWord += letter
         else:
             # This one has not been been guessed so mask it
             displayWord += maskChar
    # Display masked word
    print(displayWord)
 
    # Next we need to display any letters already guessed
    # Are there any guessed letter
    if len(guessedLetters) > 0:
        # There are, start with an empty string
        youTried=""
        # Add each guessed letter
        for letter in guessedLetters:
            youTried += letter
        # Display them
        print("You tried:", youTried)
    
    # Get a guess
    currGuess = ""
    # Make sure it's just one character
    while len(currGuess) != 1:
        currGuess = input("Guess a letter ").lower()
 
    # Don't allow repeated guess
    if currGuess in guessedLetters:
        print("You already guessed", currGuess)
    # If letter wasn't repeated
    else:
        # Save it to guessed letter list
        guessedLetters.append(currGuess)
        # And sort it
        guessedLetters.sort()
        
        # Is it a correct guess?
        if currGuess in gameWord:
            # Correct answer
            print ("Correct")
            # This letter has been guessed
            # Remove it from word letters
            wordLetters.remove(currGuess)
        else:
            # Incorrect answer
            print ("Nope")
            # One more life used
            livesUsed += 1
    
    # A little space to make it more readable
    print()
 
    # Check if user won (no more letters to guess)
    if len(wordLetters) == 0:
        # Yep!
        victory = True
    else:
        # Display remaining lives
        print (maxLives-livesUsed, "tries left")
 
# Game play is finished, display results
if victory:
    # If won
    print ("You win,", gameWord, "is correct!")
else: 
    # If lost    
    print ("You lose, the answer was:", gameWord)

Challenge 9.2

This one is a fun one. In the current game, we display the number of lives left, like this:

# Display remaining lives
print (maxLives-livesUsed, "tries left")

Can you replace that code to actually display a Hangman picture? You can use simple characters like | and / to draw one. For example, this code would print the picture at the start of the game, with no incorrect guesses yet:

print(" |---------")
print(" | / |")
print(" |/ |")
print(" |")
print(" |")
print(" |")
print(" |")
print(" |")
print(" |")
print("---")

Start with this and create the pictures needed for each wrong guess.

You’ll need an if statement to decide which picture to show.

And, here’s a tip. If you plan your print() and if statements carefully, you can do this without having to create a different picture for each number of lives. You can have one picture and change what gets shown on each line based on the number of lives left.

Oh, watch for backslash characters (the \ character). That’s a special character in Python. If you actually want to display \ as part of your hangman, you’ll want to type \ instead (you type two backslashes, but Python will display just one).

# Imports
import random
 
# Variables
maxLives = 7        # Maximum allowed tries
maskChar = "_"      # Mask character
livesUsed = 0       # Try counter
guessedLetters = [] # List to store guesses
 
#Game words
gameWords = ["anvil", "boutique", "cookie", "fluff",
            "jazz", "pneumonia", "sleigh", "society",
            "topaz", "tsunami", "yummy", "zombie"]
 
# Pick the word for the game
gameWord = random.choice(gameWords)
 
# Start the display with a fully masked word
displayWord = maskChar * len(gameWord)
 
# Actual game starts here
# Loop until guessed word correctly or out of lives
while gameWord != displayWord and livesUsed < maxLives:
 
    # First display the masked word
    print(displayWord)
 
    # Next we need to display any letters already guessed
    # Lists don't display nicely, so let's create a string
    # Are there any guessed letters?
    if len(guessedLetters) > 0:
        # There are, start with an empty string
        youTried=""
        # Add each guessed letter
        for letter in guessedLetters:
            youTried += letter
        # Display them
        print("You tried:", youTried)
 
    # Display remaining lives
    print (maxLives-livesUsed, "tries left")
    # Display hangman based on lives left
    print(" |---------")
    print(" | /      |")
    print(" |/       |")
    if livesUsed >= 1:
        print(" |       ( )")
    else:
        print(" |")
    if livesUsed == 2 or livesUsed == 3:
        print(" |        |")
    elif livesUsed == 4:
        print(" |       /|")
    elif livesUsed >= 5:
        # A product of escape characters (anything after a '\') means that to print a '\', you type '\\'
        print(" |       /|\\")
    else:
        print(" |")
    if livesUsed == 2 or livesUsed == 3:
        print(" |        |")
    elif livesUsed == 4:
        print(" |      / |")
    elif livesUsed >= 5:
        # A product of escape characters (anything after a '\') means that to print a '\', you type '\\'
        print(" |      / | \\")
    else:
        print(" |")  
    if livesUsed >= 3:
        print(" |        |")
    else:
        print(" |")
    if livesUsed == 6:
        print(" |       /")
    elif livesUsed == 7:
        print(" |       / \\")
    else:
        print(" |")
    if livesUsed == 6:
        print(" |      /")
    elif livesUsed == 7:
        print(" |      /   \\")
    else:
        print(" |")
    if livesUsed == 6:
        print(" |     /")
    elif livesUsed == 7:
        print(" |     /    \\")
    else:
        print(" |")
    print("---")

    # A little space to make it more readable
    print()
 
    # Get a guess
    currGuess = input("Guess a letter ").lower()
    # Make sure it's just one character
    while len(currGuess) != 1:
        currGuess = input("Guess a single letter ").lower()
 
    # Don't allow repeated guess
    if currGuess in guessedLetters:
        print("You already guessed", currGuess)
    else:
        # This is a new guess, save to guessed letter list
        guessedLetters.append(currGuess)
        # And sort the list
        guessedLetters.sort()
 
        # Update mask
        # Start with an empty string
        displayWord = ""
        # Loop through word
        for letter in gameWord:
            # Add letter or mask as needed
            # Has this letter been guessed?
            if letter in guessedLetters:
                # This one has been guessed so add it
                displayWord += letter
            else:
                # This one has not been been guessed so mask it
                displayWord += maskChar
        
        # Is it a correct guess?
        if currGuess in gameWord:
            # Correct answer
            print ("Correct")
        else:
            # Incorrect answer
            print ("Nope")
            # One more life used
            livesUsed += 1
    
    # A little space to make it more readable
    print()
 
# Game play is finished, display results
if displayWord == gameWord:
    # If won
    print ("You win,", gameWord, "is correct!")
else: 
    # If lost    
    print ("You lose, the answer was:", gameWord)

# Display final hangman
print(" |---------")
print(" | /      |")
print(" |/       |")
if livesUsed >= 1:
    print(" |       ( )")
else:
    print(" |")
if livesUsed == 2 or livesUsed == 3:
    print(" |        |")
elif livesUsed == 4:
    print(" |       /|")
elif livesUsed >= 5:
    # A product of escape characters (anything after a '\') means that to print a '\', you type '\\'
    print(" |       /|\\")
else:
    print(" |")
if livesUsed == 2 or livesUsed == 3:
    print(" |        |")
elif livesUsed == 4:
    print(" |      / |")
elif livesUsed >= 5:
    # A product of escape characters (anything after a '\') means that to print a '\', you type '\\'
    print(" |      / | \\")
else:
    print(" |")  
if livesUsed >= 3:
    print(" |        |")
else:
    print(" |")
if livesUsed == 6:
    print(" |       /")
elif livesUsed == 7:
    print(" |       / \\")
else:
    print(" |")
if livesUsed == 6:
    print(" |      /")
elif livesUsed == 7:
    print(" |      /   \\")
else:
    print(" |")
if livesUsed == 6:
    print(" |     /")
elif livesUsed == 7:
    print(" |     /     \\")
else:
    print(" |")
print("---")

Challenge 10.1

Want to make this a bit more interesting? Asking the user for year, month, and date (or hard coding those values) makes the math easier. But, in truth, you only need month and day, as you can figure out the year yourself: It is either this year if the birthday has not occurred yet
or next year if it has.

So update the code to prompt for a month and day and do the math to figure out the year.

import datetime

# Get today
today = datetime.datetime.now()

# Get birthday from user
mm = int(input("Enter birth month as number: "))
dd = int(input("Enter day of month of birthday: "))

# Check if currently is birthday
if today.month == mm and today.day == dd:
    print("Happy birthday!")
else:
    # Check if birthday has passed this year. Set year for birthday accordingly
    # This if asks "if current month is later, or current month is same and day is later"
    if today.month > mm or (today.month == mm and today.date > dd):
        yy = today.year + 1
    else:
        yy = today.year
    print("You birthday is in", datetime.datetime(yy, mm, dd) - today)

Challenge 10.2

Want to make it more interesting? Here are some ideas:

  • Ask the user to rate the service and pick a tip amount for them based on the reply. You can use 15% for average service, 20% (or more) for great service, and 10% (or less—maybe even 0%) for poor service.
  • Another enhancement would be to help the user split the bill. Ask them how many diners there were and then tell the user how much each needs to pay.
# Enter the bill and rate the service
billAmt = float(input("Enter amount of the bill: "))
num = int(input("How was the service (1 - 5 stars): "))

# How many diners
diners = int(input("How many diners: "))

# Choose tip based on stars
if num == 2:
    tipPnt = 10
elif num == 3:
    tipPnt = 15
elif num == 4:
    tipPnt = 18
elif num == 5:
    tipPnt = 22
else:
    tipPnt = 0

# Set tip based on bill and total based on tip and bill
tip = tipPnt/100 * billAmt
total = billAmt + tip
# Display results
# Rounding is optional, but 'round(num, 2)' will return num rounded to two decimal places
# If you round, some of the decimals may not work out well
print("The bill is $", round(billAmt, 2), "and the tip is $", round(tip, 2), "totaling to $", round(total, 2))
# How much must each person pat
print("Each diner must pay $", round(billAmt/diners, 2))

Challenge 10.3

Ok, heads-up, this one is tricky. But, we have faith in you.

Have you ever seen websites that give you password rules? They’ll say something like “Passwords must be at least 8 characters in length and have at least 1 digit and 1 special character.”

So, suppose the user says yep, I want uppercase, lowercase, digits, and special characters in my password. Easy, you pick random characters and build a password. Right?

Well, if you pick random characters from all the options, there is no guarantee that you’ll get a digit or a special character. Actually, you may not even get letters at all. You could end up with just digits or special characters.

Ideally, if the user says they want digits, you’ll make sure that there is at least 1 digit. Same for special characters. So, how could you modify the code to do this?

# Generate a password based on options provided by user.
# Minimum length is based on options selected.
# To handle required options the code always picks at
# least one of each option type. All other characters are
# random from the full allowed set and then shuffled.

# Imports
import random, string

# Function to prompt for an option
# Pass it text for input() prompt
# Returns True or False
def promptOption(prompt):
    # Initialize result
    result = False
    opt = " "
    # User must choose Y or N
    while not opt in "YN":
        opt = input(prompt).strip().upper()
    # If user entered Y then set result to True
    if opt == "Y":
        result =True
    # Return it
    return result

# Function to prompt for password length
# Pass it the minimum allowed
# Returns a length
def promptLength(minLen):
    # Make sure minimum length isn't 0, if it is then make it 1
    if minLen == 0:
        minLen = 1
    # Initialize result
    result = 0
    # Build prompt
    prompt = "How long should the password be? (Min=" + str(minLen) + "): "
    # Loop until have a valid length
    while result < minLen:
        # Prompt for length
        p=input(prompt).strip()
        # Make sure it's a valid number
        if p.isnumeric():
            result=int(p)
    # Return it
    return result

# Password chars list (stores the randomly generated characters)
# We store them in a list instead of a string as lists are easier to shuffle
pwList = []

# Characters to use (start with lowercase only)
# We'll add to this based on options selected
pwChars = string.ascii_lowercase

# Minimum password length is based on selected options
pwMinLen = 0

# Welcome message
print("I'll help you generate a great password. Ready?")

# Get options
# Uppercase
if promptOption("Include uppercase characters? [Y/N] "):
    # Increment minimum length
    pwMinLen += 1
    # Make sure have at least one uppercase
    pwList.append(random.choice(string.ascii_uppercase))
    # Add to allowed chars
    pwChars += string.ascii_uppercase
# Digits
if promptOption("Include digits? [Y/N] "):
    # Increment minimum length
    pwMinLen += 1
    # Make sure have at least one digit
    pwList.append(random.choice(string.digits))
    # Add to allowed chars
    pwChars += string.digits
# Symbols
if promptOption("Include symbols? [Y/N] "):
    # Increment minimum length
    pwMinLen += 1
    # Make sure have at least one uppercase
    pwList.append(random.choice(string.punctuation))
    # Add to allowed chars
    pwChars += string.punctuation


# Get desired length
length = promptLength(pwMinLen)

# Loop and generate password characters
while len(pwList) < length:
    # Add next character
    pwList.append(random.choice(pwChars))

# Randomize the order, this is to make sure that any required
# characters are not always first
random.shuffle(pwList)

# Loop through list and build the result string
result = ""
for c in pwList:
    result += c

# Display password
print("Your password is:", result)

Challenge 11.1

Superheroes often need to travel great distances, and depending on where they go they’ll need to measure those distances in miles or kilometers. Create two functions:

  • miles2km() accepts a distance in miles and returns that distance in kilometers.
  • km2miles() does the reverse, accepting a distance in kilometers and returning it in miles.
    Each of these functions can be written in just two lines of code.

The first is the def that defines the function and argument, and the second performs the calculation and returns it.

And to save you time, there are 1.6 kilometers in a mile, and .6 miles in a kilometer (rounded to keep things simple).

# Convert mi to km
def miles2km(num):
    return num * 1.6

# Convert km to mi
def km2miles(num):
    return num * 0.6

# Use functions
miles = int(input("Enter a value in miles: "))
print(miles, "miles = ", miles2km(miles), "km")
km = int(input("Enter a value in km: "))
print(km, "km = ", km2miles(km), "miles")