Python Forum
Make a range provide one output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make a range provide one output
#1
I know this is a simple answer, but in the below code, how do I make it only produce the final answer and not a list of each step? If you see below, the program produces the correct answer to the program I wrote but I don't want it to keep saying "the sum of cubes....". I just want it to provide the final answer. Also, how would I make the line input the chosen number instead of "n". So for example, if a user enters the number 5 into the initial prompt, how do I make the output text reflect the input provided by the user?
Ignore the "size" entries- I messed up the text upon import and now I can't get those to go away.
def main():
    print("This program calculates the sum of the cubes of the first n natural numbers")
    natn = eval(input("Enter the ending natural number: "))

    for natn in range (1, natn + 1):
        natnsum = natn ** 2 * (natn + 1) ** 2 / 4
        print("The sum of the cubes of the first n natural numbers is", natnsum, ".")

main()
Result of using the number 3 (correct):
Output:
This program calculates the sum of the cubes of the first n natural numbers Enter the ending natural number: 3 The sum of the cubes of the first n natural numbers is 1.0 . The sum of the cubes of the first n natural numbers is 9.0 . The sum of the cubes of the first n natural numbers is 36.0 . Process finished with exit code 0
-Thanks
Reply
#2
Some changes,do not use eval.
Move print() out of loop,and using string formatting.
def main():
    print("This program calculates the sum of the cubes of the first n natural numbers")
    natn = int(input("Enter the ending natural number: "))
    for natn in range (1, natn + 1):
        natnsum = natn ** 2 * (natn + 1) ** 2 / 4
    print("The sum of the cubes of the first {} natural numbers is {}".format(natn,natnsum))

main()
Reply
#3
Excellent, that works exactly as I wanted it to. I see my error- I had the print within the loop, so it was spitting out every single iteration of the loop as opposed to the final result. Also makes sense to not use eval, since I only want to allow integer values.

Thanks again
Reply
#4
(Jan-30-2017, 05:25 PM)rattlerskin Wrote: Also makes sense to not use eval, since I only want to allow integer values.

No, that's not the reason why.  eval() is a very dangerous beast, and shouldn't be used for... anything.  If you're using it, you should rewrite what you're doing to stop using it.
Reply
#5
Eval() will evaluate whatever input it gets. If one type a Python code as an input it will be executed. It can steal your ssh keys for example.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  "List index out of range" for output values pegn305 3 5,292 Nov-26-2017, 02:20 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020