1

Can someone please look at this and tell me what's wrong? I tried running it but when you say no for raining and no for snowing , it still prints out the if true statement You can't ride your bike

raining = input("Is it raining right now?\nYes or No?\n")
if raining.lower() == "yes" :
    raining = True
else:
    raining = False
snowing = input("Is it snowing out?\nYes or No\n")
if snowing.lower() == "yes" :
    snowing = True
else:
    snowing = False
if raining or snowing == True :
    print("You can't ride your bike")
if raining and snowing == False :
    print("You can ride your bike")

cost = input("How much is your new PC?")
cash = input("How much money do you have?")
total = float(cash) + float(cost)
if total < 0 :
    print("You can't buy it")
if total >= 0 :
    print ("You can buy it")
eddie
  • 1,252
  • 3
  • 15
  • 20
Zebert
  • 21
  • 2
  • Do you know `if raining or snowing == True` is interpreted as `if (raining) or (snowing == True)` not `if raining == True or snowing == True`? – Ashwini Chaudhary Aug 09 '15 at 06:46

2 Answers2

2

if raining and snowing == False is interpreted as:

if raining == True and snowing == False

You should update your second if statement as follows:

if raining == False and snowing == False:
    ...

Since if raining and if raining == True are identical for checking boolean values, you can simplify your logic like this:

if raining or snowing:
    print("You can't ride your bike")
else:
    # implies "not (raining or snowing)" or "(not raining) and (not snowing)"
    print("You can ride your bike")
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
1

Just change the and condition to elseand use brackets in the or condition.

raining = input("Is it raining right now?\nYes or No?\n")

if raining.lower() == "yes" :
    raining = True
else:
    raining = False
snowing = input("Is it snowing out?\nYes or No\n")

if snowing.lower() == "yes" :
    snowing = True
else:
    snowing = False
if (raining or snowing) == True :
    print("You can't ride your bike")
else:
    print("You can ride your bike")

cost = input("How much is your new PC?")
cash = input("How much money do you have?")
total = float(cash) + float(cost)
if total < 0 :
    print("You can't buy it")
if total >= 0 :
    print ("You can buy it")
Vineet Kumar Doshi
  • 4,250
  • 1
  • 12
  • 20