Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Marian Esskaros
Tutoring_28_10_2021_Solutions
Commits
8f7b3778
Commit
8f7b3778
authored
Oct 29, 2021
by
Fabian Benc
Browse files
Added last two solutions of the exercises
parent
e8c30efb
Changes
2
Hide whitespace changes
Inline
Side-by-side
For_Ex_3.py
0 → 100644
View file @
8f7b3778
"""Calculate sum of odd and even numbers in a list in python """
numList
=
[]
#create an empty list for entering numbers
evenSum
=
0
#declare and initialise a variable as evenSum=0
oddSum
=
0
#declare and initialise a variable as oddSum=0
num
=
int
(
input
(
"Enter the number of list elements: "
))
for
i
in
range
(
1
,
num
+
1
):
value
=
int
(
input
(
"Please enter the value %d: "
%
i
))
numList
.
append
(
value
)
j
=
0
while
(
j
<
num
):
if
(
numList
[
j
]
%
2
==
0
):
evenSum
=
evenSum
+
numList
[
j
]
else
:
oddSum
=
oddSum
+
numList
[
j
]
j
=
j
+
1
print
(
"The sum of even numbers in this list = "
,
evenSum
)
print
(
"The sum of odd numbers in this list = "
,
oddSum
)
\ No newline at end of file
For_Ex_4.py
0 → 100644
View file @
8f7b3778
"""Write a program that simulates tossing a coin n times and print how many times the tails and the head appeared. """
from
random
import
randint
n
=
int
(
input
(
"Input the desired number: "
))
num_tails
=
0
num_heads
=
0
for
i
in
range
(
n
):
toss
=
randint
(
0
,
1
)
if
toss
==
0
:
num_tails
=
num_tails
+
1
else
:
num_heads
=
num_heads
+
1
tails_prob
=
num_tails
/
n
*
100
heads_prob
=
num_heads
/
n
*
100
print
(
"Probability of tails: "
,
tails_prob
,
"%"
)
print
(
"Probability of heads: "
,
heads_prob
,
"%"
)
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment