No matter my input it still picks same option. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

No matter my input it still picks same option.

print ('Welcome to Hangman!') print ('Guess 10 letters!') inp = False if inp == False: inp = input() if inp == 'S' or 's': print ('Letter correct.\ Word: S___ _____.') inp = input() if inp == 'o' or 'O': #No matter what i choose it still picks choice 'o' print ('Letter correct.\ Word: So_o _____.') elif inp == 'l' or 'L': print ('Letter correct.\ Word: S_l_ L____.') elif inp == 'e' or 'E': print ('Letter correct.\ Word: S___ _e___.') elif inp == 'a' or 'A': print ('Letter correct.\ Word: S___ __a__.') elif inp == 'r' or 'R': print ('Letter correct.\ Word: S___ ___r_.') elif inp == 'n' or 'N': print ('Letter correct.\ Word: S___ ____n.') else: print ('Wrong.\ -----\ | |\ |\ |\ |\ |\ |\ |\ |\ --------')

27th Jun 2017, 4:45 AM
Iris Eye
Iris Eye - avatar
6 Réponses
+ 5
This can be tricky. The concept of False is: 'the absence of value'. By inference then, anything of value is effectively True (called 'Truthy' and 'Falsey'). Then...most languages short-circuit to the first value that satisfies the conditionals. For 'or' it is the first Truthy. For 'and' is the first Falsey. The language then reports which one satisfied the entire condition. Examples: print(True if 'o' else False) # True (Truthy) print(True if '' else False) # False (empty strings have no value) a=None; b='o'; c='O' print(a or b or c) # o (b is Truthy) print(a and b and c) # None (a is Falsey) #inp = input() inp='a' if inp=='o' or 'O': print(inp=='o') # False print(inp=='o' or 'O') # O (see next line) print((inp=='o') or 'O') # ...(False) or Truthy # O (Truthy) print(inp=='o' and 'O') # ...False and Truthy # False The 'if' runs because 'O' is truthy. * (so...you are only checking 'inp' once) *
27th Jun 2017, 5:19 AM
Kirk Schafer
Kirk Schafer - avatar
+ 5
It should be if inp == 'o' or inp == 'O':
27th Jun 2017, 12:37 PM
Martin Möhle
Martin Möhle - avatar
+ 5
@Maid Rondic : I didn't provide an answer this time because the original poster didn't seem to ask for one, but... ∆•Martin•∆'s answer works. You can also just do one of these (so letter case does not matter): if inp.lower() == 'o': ...stuff if inp.upper()=='O': ...same stuff
29th Jun 2017, 12:13 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
i put \ because i was printing an extra line o:
27th Jun 2017, 4:57 AM
Iris Eye
Iris Eye - avatar
0
can't tell if it is because you rewrote it or your copy failed, but the end of each print should be ') , not \
27th Jun 2017, 4:48 AM
Daemo
Daemo - avatar
0
The point is if you keep one correct answer it will work, i still do not realise how to make two correct
28th Jun 2017, 10:06 PM
Maid Rondic
Maid Rondic - avatar