0

python noob here. I'm sure this is an easy question.. Is it possible to get numerical data collected from multiple user input questions into a single variable? For example, if I wanted to ask a user three questions:

int(input("What is your yearly income?"))

int(input("What is your partner's yearly income?"))

int(input("Enter any additional income"))

Can you store all of that data collected directly into a single variable named total_income. What I would like is to be able to get a sum total into a single variable.

7
  • Input in a single line(separated by space) or in multiple lines? Commented Sep 9, 2017 at 19:40
  • Just my curiosity, why would you stores each of your input into a separated list in the example given?? Commented Sep 9, 2017 at 19:42
  • I just removed the square brackets. I actually forgot to remove it as I copy and pasted it. I originally had a separate variable for each question that stored the input data in a list form. I was hoping to just sum each list into one master list but that failed. Commented Sep 9, 2017 at 20:13
  • for your edit : you need to change the text in input('bliblabla') to tell the user to enter all values in the line, separated by space, don't change the suggested split() method. The solution provided by @deepansh-sachdeva is more ergonomic for you and the user... Commented Sep 9, 2017 at 20:15
  • the message should be like input("Enter your yearly income, your partner income and any additional income separated by a space").split() Commented Sep 9, 2017 at 20:30

2 Answers 2

3

This might be helpful:

t = 0

t += int(input("What is your yearly income?"))
t += int(input("What is your partner's yearly income?"))
t += int(input("Enter any additional income"))
Sign up to request clarification or add additional context in comments.

2 Comments

Deepansh Sachdeva, thank you very much. This is very simplistic and just what I needed. I was making it too complicated.
@TTom Glad to know. Also, please accept my solution if it solved your query and do upvote if you found it useful. Thanks :)
0

If your input in a single line(separated by space):

a=sum(map(int,input().split()))
print (a)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.