94.6k views
3 votes
Write a program that will print out statistics for eight coin tosses. The user will input either an "h" for heads or a "t" for tails for the eight tosses. The program will then print out the total number and percentages of heads and tails. Use the increment operator to increment the number of tosses as each toss is input. For example, a possible sample dialog might be: For each coin toss enter either ‘h’ for heads or ‘t’ for tails. First toss: h Second toss: t Third toss: t Fourth toss: h Fifth toss: t Sixth toss: h Seventh toss: t Eighth toss: t Number of heads: 3 Number of tails: 5 Percent heads: 37.5 Percent tails: 62.5

User Kotarak
by
5.2k points

1 Answer

3 votes

Answer:

Written in Python

head = 0

tail = 0

for i in range(1,9):

print("Toss "+str(i)+": ")

toss = input()

if(toss == 'h'):

head = head + 1

else:

tail = tail + 1

print("Number of head: "+str(head))

print("Number of tail: "+str(tail))

print("Percent head: "+str(head * 100/8))

print("Percent tail: "+str(tail * 100/8))

Step-by-step explanation:

The next two lines initialize head and tail to 0, respectively

head = 0

tail = 0

The following is an iteration for 1 to 8

for i in range(1,9):

print("Toss "+str(i)+": ")

toss = input() This line gets user input

if(toss == 'h'): This line checks if input is h

head = head + 1

else: This line checks otherwise

tail = tail + 1

The next two lines print the number of heads and tails respectively

print("Number of head: "+str(head))

print("Number of tail: "+str(tail))

The next two lines print the percentage of heads and tails respectively

print("Percent head: "+str(head * 100/8))

print("Percent tail: "+str(tail * 100/8))

User Jorg
by
5.3k points