-1
def adding_report(var):
    lists=[]
    total=0
    while True:
        if var.isdigit():
            total+=int(var)
            int(var).append(lists)
        elif var=="Q":
            choice=input("For total 'T' , for all 'A'").upper
            if choice=='T':
                print(total)
                break               
            elif choice=='A':
                print(lists)
                print(total)
                break                
            else:
                print("Invalid input")

while True:
    var=input("Enter an integer or Quit 'Q' ")
    if var.isdigit():
        adding_report(var)
    elif var=="Q":
        adding_report(var)
        break
    else:
        print("Invalid input")

I am trying to store the input that i got from the user. I need to store them in a one variable. I will show all the input when user wants it or i will sum up all of them and show the total result to a user. But i dont know how to store integers in a one variable.

The final list should be like this; input1 input2 input3

Total:input1+input2+input3

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • What do you mean by a one variable? – Cale Sep 24 '17 at 14:39
  • I mean i want to store each input in a only one variable, so i can use them later. For example i want to store each input in a list. – Emirhan Genç Sep 24 '17 at 14:40
  • Your code is *already* attempting to store all the integers in one variable. That variable is called `lists`. You do need to put your definition of `adding_report` before the `while` loop, not after. And you have the `append` back to front. It should be `lists.append(int(var))`. – BoarGules Sep 24 '17 at 14:44
  • I've changed the program already. But right now when i enter an integer i get an error which says "AttributeError: 'int' object has no attribute 'append'" – Emirhan Genç Sep 24 '17 at 14:45
  • I want my program to store the integers in a lists. When the user press Q program will ask "You want to see the total if so press t, or do you want to see all of the inputs you have entered and their sums then press A" – Emirhan Genç Sep 24 '17 at 14:47

3 Answers3

1

You already got a clean solution from @bhansa but, just in case you wanted to maintain the loop and sequential user inputs, as they were in your script:

def adding_report(var, values):
    if var.isdigit():
        values.append(int(var))
        return False
    elif var.lower() == 'q':
        total = sum(values)
        choice = input("For total: T , for all: A\n").lower()
        if choice in 'ta':
            if choice == 'a':
                print(' '.join([str(i) for i in values]))
            print(total)
            return True
        print("Invalid input")

if __name__ == "__main__":
    values = []
    total = 0
    while True:
        var = input("Enter an integer or Quit 'Q'\n")
        exit = adding_report(var, values)
        if exit:
            break

A quick note: you don't need a while loop in your adding_report function and you want to break the main loop, when the user types Q


EDIT:

I corrected the above code to work in Python 3 (my bad, I had missed that's the version tagged in the question; thank you @MarkTolonen for pointing that out).

To have the above code work with Python 2.7, one option is to replace input with raw_input; otherwise, a fix can be added on top, while leaving the rest unchanged (as suggested here):

try:
    # Fix Python 2.x
    input = raw_input
except NameError:
    pass

I'm no expert of multiple Python versions support, though, so there might be better solutions. Feel free to comment and/or expand, I'd be happy to hear.

mapofemergence
  • 458
  • 3
  • 7
  • The question is tagged `python-3.x`. `raw_input` is only in Python 2. `input` is its equivalent in Python 3. – Mark Tolonen Sep 24 '17 at 17:14
  • you're right @Mark, thank you for pointing that out. I just edited the question, I hope that's better formulated now – mapofemergence Sep 24 '17 at 17:53
  • Your answer is doing the work great! But my function need to have only one string parameter. But it seems like your answer improved my knowledge in python further more. Thank you so much guys, – Emirhan Genç Sep 24 '17 at 18:06
  • can you explain why you need a single string parameter (I assume you mean you do not want to pass the list)? There are alternatives, but I would avoid suggesting something which is not what you need – mapofemergence Sep 24 '17 at 18:11
0

If you just want to store inputs in a list and show total, Much simpler solution would be this:

lista = list(map(int, input().split()))

for index, item  in enumerate(lista):
  print("input{} : {}".format(index, item))

print("Total: ", sum(lista))

#  1 2 3 
# input0 : 1
# input1 : 2
# input2 : 3
# Total:  6

Put the above statements into functions according to your conditionals.

bhansa
  • 7,282
  • 3
  • 30
  • 55
0

If your question is regarding the Edx Python course, your code can look like below:

When use 'A' as the argument to adding_report() gives results in printing of all of the input integers and the total. 'T' gives results in printing only the total.

def adding_report(report):
my_sum = 0
my_rep = report
elements = ['Items']
print('Input an integer to add to the total or "Q" to quit')
while True:
    element = input('Enter an integer or "Q" for quit: ')
    if element.isdigit():
        my_sum += int(element)
        elements.append(element)
    elif element.lower().startswith("q"):
        if my_rep == 'A':
            for el in elements:
                print(el)
            print('\nTotal\n', my_sum)
            break
        elif my_rep == 'T': 
            print('\nTotal\n', my_sum)
            break
    else:
        print('"' + element + '"', 'is invalid input')


adding_report('A')
adding_report('T')
Adil B
  • 14,635
  • 11
  • 60
  • 78
2RMalinowski
  • 357
  • 5
  • 3