For Loop does not show the all answers from the input() , in python
Question:
total = 0
for i in range(5):
question = int(input("Enter a number : "))
included = input("Do you want that number included , (y/n) : ")
if included == "Y" or included == "y" :
total = total + question
print(total)
This is my code and I was trying to get each number from the question variable and add them to the total if the user writes y in the second variable which is included , but when i print total , it should add the numbers together, but the output shows the last number that the user wrote
if could someone tells me the answer to my question, how I can get each number from the input in the loop, to add them together
Answers:
Well that’s very simple. I’ve tested you program, and you just messed up the tabs.
total = 0
for i in range(5):
question = int(input("Enter a number : "))
included = input("Do you want that number included , (y/n) : ")
if included == "Y" or included == "y" : # over here
total = total + question
print(total)
You see when you type anything, while loop, for loop, if statement or anything which end’s with column ":", you should add tabs before each line of code which is related to loop or statement.
Example:
if name == 'John': <----------------------------------------------------,
print('Your name is John') <------------, |
^.________ over here we have 4 spaces so this is related to statement here
this way "print" function will work only when statement is true (if name is john).
I hope I explained it easy enough, if you still have some trouble with it, look at this tutorials here
total = 0
for i in range(5):
question = int(input("Enter a number : "))
included = input("Do you want that number included , (y/n) : ")
if included == "Y" or included == "y" :
total = total + question
print(total)
This is my code and I was trying to get each number from the question variable and add them to the total if the user writes y in the second variable which is included , but when i print total , it should add the numbers together, but the output shows the last number that the user wrote
if could someone tells me the answer to my question, how I can get each number from the input in the loop, to add them together
Well that’s very simple. I’ve tested you program, and you just messed up the tabs.
total = 0
for i in range(5):
question = int(input("Enter a number : "))
included = input("Do you want that number included , (y/n) : ")
if included == "Y" or included == "y" : # over here
total = total + question
print(total)
You see when you type anything, while loop, for loop, if statement or anything which end’s with column ":", you should add tabs before each line of code which is related to loop or statement.
Example:
if name == 'John': <----------------------------------------------------,
print('Your name is John') <------------, |
^.________ over here we have 4 spaces so this is related to statement here
this way "print" function will work only when statement is true (if name is john).
I hope I explained it easy enough, if you still have some trouble with it, look at this tutorials here