USING PYTHON PROGRAMMING AND WHILE LOOPS NOT FOR LOOPS The first step is to copy in the list of p rogramming langauges , and to
USING PYTHON PROGRAMMING AND WHILE LOOPS NOT FOR LOOPS
The first step is to copy in the list of
p
rogramming langauges
, and to write code
that will print out the different choices.
Copy the list
below into your programs
main()
:
# types of
p
rogramming languages
to vote on
languages = [
"Python"
,
"Java"
,
"C++"
,
"Ruby"
,
"C"
,
"PHP"
,
"Shakespeare"
]
To print the choices, you should write a while loop
that will print out the
following two things on each line:
The number of the choice (with the count starting at 1, not 0!)
The programming languages name.
This is the second of
four
steps that must be written for this lab.
Now that the
code to display the
p
rogramming languages
i
s complete
, we need
to allow the user and their friends to actually vote!
For now,
we wont worry about storing the votes
. Just write the code that will
allow the user to vote, and will stop when they enter a 0 to quit.
If they make an invalid choice, simply ignore it and ask again
This is the third
of four steps that must be written for this lab.
Now that you can accept votes, we need to store them.
Well store the votes for the programming languages
in another, separate list of integers
.
So if, for example, the user voted for
Python (choice 1),
Shakespeare twice (choice 7)
and
C
++
four times
(choice 3), the vote list would look like this:
votes =
1
0
4
0
0
0
2
index 0
index 1
index 2
index 3
index 4
i
ndex 5
index 6
In other words, the votes
stored
at a give
n index
in
votes
should be
for the
p
rogramming language
stored
at that same index in the
l
anguages
list.
Remember, list indexing starts at 0, but were presenting the choices to the
user starting at 1, so the way you store votes will need to compensate for this
offset. Youll also need to make sure you
i
gnore
invalid input
dont
t
ry to
count a vote for option #
8
, when there are only 7 choices!
At the end, print out the list of votes, so you can ensure your program is
working correctly.
(Simply use
print(
"votes:",
votes)
in your code.)
This is the last of
four
steps that must be written for this lab.
This last step is relatively simple, as
youve already done all of the h
ard work.
For this step, well display the final votes for each
p
rogramming language
. (If
your code that asks for and stores votes doesnt work correctly, you might also
have to do some debugging. Thats how programming works, sometimes!)
Once the user has ent
ered 0 in order to stop voting, you need to go through
the list and print out the number of votes each
p
rogramming language
earned.
You will need to iterate through
both of the lists,in the same while loop,
inNorder to print out the programming lang
uage
and the number of votes it
receiv
ed
SAMPLE OUTPUT
bash
-
4.1$ python
l
anguages
.py
[[
p
rogramming
l
anguages
should be displayed here ]]
What
language do you
like? (Enter 0 to stop):
3
What
language do you
like? (Enter 0 to stop):
3
What
language do you
like? (Enter 0 to stop):
4
What
language do you
like? (E
nter 0 to stop):
6
What
language do you
like? (Enter 0 to stop):
0
P
ython
has 0 votes.
J
ava
has 0 votes.
C
++
has 2 votes.
R
uby
has 1 votes.
C
has 0 votes.
P
HP
has 1 votes.
S
hakespeare
has 0 votes.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started