 Phyton Powers Loop

Hi! I’m very confused and I need help on this assignment!

I need to create a loop for a powers program (not using pow)

First, this needs to show up!

1. Square it (x^2)
2. Cube it (x^3)
3. Power (x^y)
4. Exit

Then if the users enter 1, 2, or 3 it will ask for a number and output it squared then display the menu again. It must repeat until option 4 is selected.

If user enters 4 it will end the program and send a farewell.

The program is required to use a loop to output the number to the power of the (exponent) value through repeated multiplication.

I’m genuinely so confused, and Im just in high school taking my first computer science class. Could I have some help? Thanks!

Hi Keira,

When faced with a large programming task, break it down into smaller
pieces, and tackle each piece one at a time.

Start by writing a loop that asks the user for a number 1-4.

If they enter 1, just print “Square it”. For 2, print “Cube it”, and for
3, print “Power it”. We will worry about actually doing the powers
later. For 4, exit the loop. If you set up your while loop correctly,
that will happen automatically.

Can you start by doing that?

Hints:

• you can ask the user for a number with:

the_number = int(input("Gimme a number!!! "))

• you can write a loop with:

while the_number != 4:
# ask the user for a number
# print some stuff depending on the number

Can you get that working? Start with that, and show us what you get, and
then we’ll move onto the second part, which is to actually do the
powers.

1 Like

How you tried any code at all? Have they given you any example code?

We don’t like to provide solutions (you don’t learn from them) but are
happy to provide suggestions and explainations and critique of things
you have tried (include the code and errors inline for this).

I’m going to have to provide some code for this though.

The menu part is usually written as a while loop, seems to be a standard
exercise for introductory programming:

while True:
print("1. Square it (x^2)")
... print the other menu choices here ...
choice = int(input("prompt string here> "))
if choice == 1:
... act on choice 1 here ...
elif choice == 2:
... and so on ...
elif choice == 4:
break
else:
print("Unsupported choice:", choice)

That gets you that main outline: a loop while runs forever (because
“True” is always true), a menu printout, an input of a number.

This doesn’t ask for the number to work on yet.

Note that the input() function returns a string. We convert it to an int
for comparison. You could, alternatively, keep the string (get rid of
the int() bit) and compare to the string, as they’re just labels really.

if choice == "2":   # compare to a string

Start with this. Replace the “… blah …” bits with trivial code such
as:

print("code for choice 2 goes here")

Then you need to write the power stuff. Do this second once the loop
works. It is usually best to make stings work in stages as otherwise
you’re trying to find problems with many moving parts.

Write down how you’d compute a power by hand, eg a cube.

Can you write a function in Python to do that in a general way? i.e. a
function like pow() accepting the number and the power?

def my_pow(x, y):
... compute x^y here ...
return the result

They must have told you something about for-loops. Can you write a loop
which runs a certain number of times? eg “y” times? Does that help