×

Challenges

Challenge 4.3

# Count dots on 5 black and white cards as one input (without a loop)

Challenge Level: Growing experience

## Requirement:

Write a program that asks the end user to enter 5 black and white cards representing bits, all as one line of input ('B' for black and 'W' for white), and displays the total number of dots as the output. You can do this without a loop, using 5 similar sets of blocks to process each of the 5 values.

Hints
• Make a variable called total_number_of_dots and set its value to 0. Make a variable called cards and set its value to the input entered by the end user (5 black and white cards).
• Check each letter of the string and if it’s ‘W’ add the corresponding number of dots (16 for the first letter, 8 for the second letter and so on) to the total_number_of_dots.
• Display the total_number_of_dots as the output.
• You can access a letter at the specified position in a string by using cards[index], where index is the position of the character you want. For example, if some_string = 'word', some_string[2] is 'r' and some_string[0] is 'w'.
Programming Reminders
# Print a string directly
print("Hello World!")

# Print a variable
print(my_var)

#### Variables

# Set a variable as a string
fruit_name = "Apple"

# Set a variable as an integer
pieces_of_fruit = 7

# Set a variable from a calculation
cost_of_fruit = pieces_of_fruit * cost_per_item

# Add one to a value
pieces_of_fruit += 1

#### Conditionals

# Find out the discount on fruit
if pieces_of_fruit > 100:
print("Bulk discount applies")
elif pieces_of_fruit > 5:
print("Discount applies")
else:
print("No discount")

#### For loops

# Print numbers 0-9 - remember Python starts counting from 0
for num in range(10):
print(num)

#### While loops

# Print numbers 0-9 using a while loop and a variable
num = 0
while num < 10:
print(num)

# Increment the variable by one.
# It will prevent an infinite loop!
num += 1 

#### Lists

# Create a list of fruit
fruit = ["Apple", "Banana", "Orange", "Pear"]

#### Functions

# Create a function which prints a greeting
def greeting(name):
print("Hello " + name)

# Call the function
greeting("Spiderman")

Enter your code in the editor below

Your results will be displayed here

Input Expected output Received output Status
WWBBB
24

Not yet run ?
BBBBB
0

Not yet run ?
WWWWW
31

Not yet run ?