if statement - Python If/Else Only Returning In Order, Not By Logic -


resolved

when integer containing "0" or "4" entered, if-statement returns first in statement.

for example, in code below, if enter "60", execute:

print "nice, you're not greedy - win!" exit(0)

not

dead("you greedy bastard!")

as expected how_much >= 50.

have tried bunch of changes, can't seem execute intended. know what's going on here?

def gold_room():     print "this room full of gold. how take?"     number_type = false      while true:          choice = raw_input("> ")          how_much = int(choice)          if "0" in choice or "4" in choice , how_much < 50:             print "nice, you're not greedy - win!"             exit(0)         elif "0" in choice or "4" in choice , how_much >= 50:             dead("you greedy bastard!")         else:             print "man, learn type number. put 0 or 4 in number." 

you have order-of-operations issue. and operator binds more tightly or operator, when write:

  if "0" in choice or "4" in choice , how_much < 50: 

you getting:

  if ("0" in choice) or ("4" in choice , how_much < 50): 

and hopefully, parentheses, it's obvious why entering 60 triggers "nice, you're not greedy - win!" message (because matches "0" in choice coindition, , since condition true, entire or statement true).

add parentheses want:

  if ("0" in choice or "4" in choice) , how_much < 50: 

see this article details.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -