Your church youth group is hosting a “Favorite Bible Character” Voting Contest to inspire young members to learn more about biblical figures. Participants can vote for one of four beloved Bible characters: David, Esther, Moses, or Ruth.
The aim is to make the voting process fair and transparent while ensuring everyone can view the results in a visually engaging way.
Task:
Write a Python program using Streamlit to create a voting app.
Menu options:
Create a sidebar with two options:- “Cast Your Vote” for participants to vote.
- “View Results” to display the current standings.
Voting process:
- Ask the user for their name and their favorite Bible character.
- If the participant has already voted, display a message to prevent duplicate votes.
- Save valid votes to a CSV file.
Display results:
- Count votes for each character.
- Show the results as a table and visualize them as a bar chart using Plotly.
Steps to Solve:
Create the app:
- Use Streamlit to build the app interface.
- Initialize or load a CSV file (
bible_votes.csv
) to store the voting data.
Design the voting menu:
- Add input fields for the participant’s name and character selection.
- Check for duplicate votes by verifying if the name already exists in the CSV file.
- Save new votes to the CSV file.
Design the results menu:
- Count the votes for each Bible character using pandas.
- Display the results in a table.
- Use Plotly to create a bar chart to visualize the results.
Handle exceptions:
- Display a warning if no votes have been cast yet.