Reputation: 33
I'm learning python and got stuck on the if statement. I'm trying to total the answers to the input statements if I enter Yes to all 3 questions, but instead I get YesYesYes, instead of 0.60. Code below:
Question_1=input("Did you look at the OCC strategy in the line chart? ")
if Question_1=="Yes":
print (0.2)
else:
print (0)
Question_2=input("Are you trading in the same direction as the 20 day moving average? ")
if Question_2=="Yes":
print (0.2)
else:
print (0)
Question_3=input("Are you trading in the same direction as the 50 day moving average? ")
if Question_3=="Yes":
print(0.2)
else:
print(0)
Total=(Question_1 + Question_2+Question_3)
print(Total)
Upvotes: 0
Views: 26
Reputation: 8589
Here's a possible solution:
question_1 = input("Did you look at the OCC strategy in the line chart? ")
result_1 = 0
if question_1.lower().strip()=="yes":
result_1 = 0.2
question_2 = input("Are you trading in the same direction as the 20 day moving average? ")
result_2 = 0
if question_2.lower().strip()=="yes":
result_2 = 0.2
question_3 = input("Are you trading in the same direction as the 50 day moving average? ")
result_3 = 0
if question_3.lower().strip()=="yes":
result_3 = 0.2
total = result_1 + result_2 + result_3
print(total)
The main issue was that you were just printing the result values and not storing the result in a variable and in total you were printing only the content of question_x (the user's input).
I solved this issue also removing the else pre_setting the value of result_x to 0.
As added thing I used .lower() (to make the text small caps) and .strip() (to remove the extra spaces in the beginning / end) to make sure that if the user inserts spaces or is using YES / Yes / etc it will work in any case.
Please also try to use Python style guide when you write variable names, it will make your code more readable by others and more Pythonic.
Upvotes: 1