A community for students. Sign up today!
Here's the question you clicked on:
 0 viewing
 one year ago
Anybody care to check this code and make sure it's correct? It works in any situation I've tried, looking for holes....
Question from OCW 6.189, Homework 1.8 #3:
Write a program using a for loop that calculates exponentials. Your program should ask the user for a base 'base' and an exponent 'exp', and calculate base^exp.
#if y is negative (< 0) then it is 1/x*x
#if y is positive (> 0) then it is x*x
#if y = 0 then answer is 1
x = input("Choose your base: ")
y = input("Choose your expo: ")
if y>0:
print x**y
elif y<0:
print 1/x**y
else:
print 1
 one year ago
Anybody care to check this code and make sure it's correct? It works in any situation I've tried, looking for holes.... Question from OCW 6.189, Homework 1.8 #3: Write a program using a for loop that calculates exponentials. Your program should ask the user for a base 'base' and an exponent 'exp', and calculate base^exp. #if y is negative (< 0) then it is 1/x*x #if y is positive (> 0) then it is x*x #if y = 0 then answer is 1 x = input("Choose your base: ") y = input("Choose your expo: ") if y>0: print x**y elif y<0: print 1/x**y else: print 1

This Question is Open

Taufique
 one year ago
Best ResponseYou've already chosen the best response.0in your question,calculate exponential using for loop..so try following steps to find exponential.. for num in range(1,exp): # loop will move from 1 to exponet value. value=value*base #multiply base exp times print 'the exponential value is* %d' % (value)

boogiepicker31
 one year ago
Best ResponseYou've already chosen the best response.0Ok, here's the revised code. I've tested it a bunch of times, and it all works: x = input("Choose your base: ") y = input("Choose your expo: ") value = 1 for num in range(y): value = value*x print value However I still need some help. First of all, is there a more succinct/efficient way of writing this code? I'm sure there is. Secondly, I'm confused as to why my old code wasn't working. My old code looked like this: x = input("Choose your base: ") y = input("Choose your expo: ") for x in range(y): x = x*x print x And I would get answers like this: base = 3 expo = 3 0 1 4 I'm just wondering why it would give me those answers. Thanks for the help in advance, you guys are awesome.
Ask your own question
Ask a QuestionFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.