0

The mission is to get the user to input some value if extra cash was spent to replace a capital good, but if no extra cash was spent, then I want the program to move on to the next line of code.

Where did I go wrong with my code?

print("Did you spend extra to replace a capital goods\n")

variableExpense = 'Yes'
variableExpense = 'No'

if variableExpense == 'Yes':
    variableExpense = input('How much did you spent: \n')
else:
    if variableExpense == 'No':
    print('print some statement.\n')
Emmanuel Simon
  • 3
  • 1
  • 1
  • 4
  • `variableExpense = 'Yes'` and then `variableExpense = 'No'` makes no sense. You are overwriting the variable immediately – Krishna Nov 26 '18 at 04:54

1 Answers1

1

You are currently assigning variableExpense to the value Yes, then in the next line you set it to No, so the following statement if variableExpense == 'Yes' will never evaluate to true. Try it like this

print("Did you spend extra to replace a capital goods\n")

variableExpense = input("Yes or No: ")

if variableExpense.lower() == 'yes':
    extraSpent = input('How much did you spent: \n')
else:
    if variableExpense.lower() == 'no':
        print('print some statement.\n')
M.G
  • 440
  • 1
  • 3
  • 10
  • Thanks for the help, but when I ran the code and enter yes it skips line 6. – Emmanuel Simon Nov 26 '18 at 05:02
  • That's because currently the input that it is checking is case sensitive. You must type "Yes" instead of "yes". To fix this you can use [`lower()`](https://www.tutorialspoint.com/python/string_lower.htm) which I will leave for you to attempt to implement. If you have issues with this I will edit post later with an example that has it implemented. – M.G Nov 26 '18 at 05:06
  • Once again I thank you, your answer was very helpful to my code. I would like to also see the example so I can gain more practice on this. – Emmanuel Simon Nov 26 '18 at 05:17
  • @EmmanuelSimon: You might find this interesting: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Matthias Nov 26 '18 at 08:46
  • I will implement the program with your suggestion @Matthias and see how it works. – Emmanuel Simon Nov 26 '18 at 15:18