Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:32:36+00:00 2026-06-14T12:32:36+00:00

I’m trying to make my first Python calculator which can add given values given

  • 0

I’m trying to make my first Python calculator which can add given values given by user. The problem is that sometimes we may have several values to add i.e. a + b is not only the addition we have a + b + d + g + h + ... so I have defined a range up to n where n is the user input.

Now the problem is that if the user gives a value of 5 in the range, then how to map the each and every value in that range to enter the values to add?

The code:

 def main():
    print("how many no.s are we dealing with?");
    n=int(input(""));
    for i in range(n):
        print("addition:");

Cutting things short, I just want a user to first type how many values would be adding and then the user has to type all those values to be typed for the calculator to add them.

It’s like if the user has a range of 3 numbers [a + b + c] then the user would type 3 in the first prompt then he would type a, b, c values in each prompt to give out the total.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:32:38+00:00Added an answer on June 14, 2026 at 12:32 pm
    def main():
        n=int(input("how many no.s are we dealing with?"))
        result = 0
        for i in range(n):
            value = float(input('enter next number to add'))
            result += value
        print('the result is {0}'.format(result))
    

    Notice the lack of ;

    How this works is that the result is initialized to 0. Then every time the loop iterates the user is asked for a number. That number is then added to the result.


    There is, however, an easier way:

    sum_string = input('please enter a sum. for example: "1 + 2 + 10 ..."') #1
    result = sum([float(i) for i in sum_string.split('+')])                 #2
    print('the result is {0}'.format(result))                               #3
    

    How this works is:

    in line 1: you ask the user to enter a string and store it

    in line 2: you do a whole lot… I’ll examine it piece by piece:

    • sum_string.split('+') takes the string inputted by the user and turns it into a list. for example: 1+2+ 45'.split('+') => ['1','2',' 45']
    • continuing from the example above: [float(i) for i in ['1','2',' 45']] => [1,2,45]
      this is called list comprehension. It is awesome and totally worth looking up
    • lastly sum([1,2,45]) => 48`

    in line 3: we print out the result. I’m not really sure what you want to do with it

    Note: this will not work with negative numbers as it stands but can be adapted to do so…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.