×

Challenges

Challenge 4.5

# Count dots on any number of black and white cards as one input (using a loop)

## Requirement:

Write a program that asks the user to enter any number of black and white cards representing bits, all as one input ('B' for black and 'W' for white), and displays the total number of dots as the output.

Hints
• Make variables called:

• total_number_of_dots and set its value to 0.
• number_of_dots and set its value to 1. You will need to multiply number_of_dots by 2 each time you go through the loop
• cards and set its value to the string entered as the input. Check each letter of the input string cards (use a loop to iterate the number of letters in the input) starting from the last letter. If it’s ‘W’ add the corresponding number of dots (1 for the last letter, 2 for the second to last letter and so on) to the total number of dots. Display the total_number_of_dots as the output.
• i and set its value to the number of letters in the input minus 1. Use this variable to access a letter at the i position in the string.
• You can access a letter at the specified position in a string by using brackets, like cards[i]

• In this challenge you need to access all the letters in user’s input and check to see if each of them is equal to B (black) or W (white).

• You can find how many letters a string has by using len(cards)

• In this challenge you need to check if each letter of the input is equal to ‘W’ or B’ using and if statement

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
WBB
4

Not yet run ?
W
1

Not yet run ?
B
0

Not yet run ?
WWWWWWWW
255`

Not yet run ?