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")
to start with. Make sure the loop works.
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.
Add a call to ask for a number.
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
adapting your paper version to code?
Cheers,
Cameron Simpson cs@cskk.id.au