Registration

A program is required to take attendance at an event.

Write a program that will loop through an array of names. The user will then be asked if the person is at the event. For each person in the array, the user can answer yes or no. Answering yes will count how many people are at the event. At the end of the program, the total attendance should be displayed.

Is John at the event? No
Is Amy at the event? Yes
Is David at the event? Yes
There are 2 people at the event
DECLARE names INITIALLY ["John", "Amy", "David"]
DECLARE totalAttending INITIALLY 0

FOR i FROM 0 TO LENGTH(names) - 1 DO
    SEND "Is " & names[i] & " at the event? " TO DISPLAY
    RECEIVE hasAttended FROM (STRING) KEYBOARD

    IF hasAttended = "Yes" THEN
        totalAttending = totalAttending + 1
    END IF
END FOR

SEND "There are " & totalAttending & " people at the event" TO DISPLAY
names = ["John", "Amy", "David"]
total_attending = 0

for i in range(len(names)):
    question = "Is " + names[i] + " at the event? "
    has_attended = str(input(question))
    
    if has_attended == "Yes":
        total_attending = total_attending + 1

print("There are", total_attending, "people at the event")
# Creates an array with the names John, Amy and David
names = ["John", "Amy", "David"]

# Create a running total starting at 0
total_attending = 0

# Loops through all of the names
for i in range(len(names)):
    # Using Concatenation to form the question
    question = "Is " + names[i] + " at the event? "
    # Asking the user if someone has attended, using the formed question
    has_attended = str(input(question))
    
    # If the user entered "Yes"...
    if has_attended == "Yes":
        # Increases total_attending by 1
        total_attending = total_attending + 1

# Display the total number of people at the event to the user
print("There are", total_attending, "people at the event")

Extension

Extend to the program to include the following:

  • Input validation that ensures the user either enters Yes or No
  • Add 3 additional names to the array
Is John at the event? Yes
Is Amy at the event? No
Is David at the event? Yes
Is Mary at the event? Yes
Is Sophie at the event? N
Must enter either Yes or No
Is Sophie at the event? No
Is Andrew at the event? Yes
There are 4 people at the event

Target

You should be able to use 1-D Arrays to solve a problem.