123k views
1 vote
Create a program that ask the user to enter the number of switches and calculates the

possible outcomes that can come from those many number of switches (on / off).

Follow the steps given below to achieve the desired result:

● Step 1​: Get the number of ​switches​ from the user and store it in a variable (lets

say ​switches).

● Step 2​: Initialize 3 variables ​max_poss = 2, ​i = 1 and ​power = 1, where,

○ max_poss = 2 denotes the maximum possibile outcome of 1 switch (i.e. ​on

and ​off​),

○ i is used to set the condition in ​while loop and it will keep incrementing

unless all the switches are over.

○ power variable is further used to store the result.

● Step 3​: Define a ​while loop with a condition to execute till the value of ​i is less

than or equal to ​switches.

● Step 4​: Inside the ​while loop,

○ Multiply the value stored in ​max_poss with ​power and store the result in

power.

○ Increment the value of ​i by ​1.

For eg: When ​switches = 2, In the

1st ​iteration, ​power = 2 and in the ​2nd​ iteration, ​power = 2 * 2 i.e. ​4. This indicates that, the

number of possible outcomes = ​2switches​, where ​switches denotes the number of

switches.

● Step 5​: Print the total number of possible outcomes stored in variable ​power.
plss help me with this​

User Yrral
by
6.7k points

1 Answer

4 votes

In python:

switches = int(input("How many switches? "))

max_pos = 2

power = 1

i = 1

while i < switches:

power = max_pos * power

i += 1

print(f"The total number of outcomes is {power}")

I hope this helps!

User JackCColeman
by
7.6k points