Commit 8f7b3778 authored by Fabian Benc's avatar Fabian Benc
Browse files

Added last two solutions of the exercises

parent e8c30efb
"""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
"""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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment