Stop if statement from running - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Stop if statement from running (/thread-26557.html) |
Stop if statement from running - KieranPage - May-05-2020 im knew to python and have started work on a small project import random strength = random.randint(1,100) health = random.randint(1,100) energy = random.randint(1,100) Text1= 'Your strength is ' Text2= 'Your health is ' Text3= 'Your energy is ' Text4= 'You took ' Text5= ' damage' Text6= 'your health is now ' Text7= 'your energy is' damage = random.randint(1,20) print (Text1 + (str(strength))) print (Text2 + (str(health))) print (Text3 + (str(energy))) print ('You see a beast before you what are your actions') action = input ('Fight or Run') if action =='Fight' or 'fight': print ('You Fought') print (Text4 + (str(damage)) + Text5) if action =='Run' or 'run': print ('you ran') print (Text7 + (str(energy-20)))but when i run this code it gets to the points where it asks fight or run. If i choose fight it works perfectly fine but if i choose run it still does the fight sequence and then the run sequence and i do not know how to fix this (i know my code isnt the best but i understand it) RE: Stop if statement from running - tonytech - May-05-2020 removed response to avoid confusion.. RE: Stop if statement from running - stullis - May-05-2020 The main issue is the conditional: if action =='Fight' or 'fight': # Evaluates to if action == 'Fight' or True:The string literal will always be True. As a result, the or expression will always be True. To fix it, you need can do one of the following: if action =='Fight' or action == 'fight': # Or if action in ('Fight', 'fight'): # Or if action.lower() == 'fight': RE: Stop if statement from running - Yoriz - May-05-2020 @KieranPage The if statements don't do as expected because of the or , see the following linkMultiple expressions with "or" keyword note rather than checking for various case you could just turn the input into lowercase. action = input('Fight or Run') action = action.lower() if action == 'fight': ...@tonytech That's great that you got the code working but note you have added duplication of code for each outcome |