Quantcast

A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

boogiepicker31

  • 2 years 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
  1. Taufique
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    in 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)

  2. boogiepicker31
    • 2 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Ok, 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.

  3. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.