Code for creating game with Python using If and Elif statements and Functions:
Rules for this program is:
Computer choose any random value using random.randit function. When it choose 1 it will be equal to ‘s’, 2 = ‘w’ and 3 =’g’. You have to enter ‘s’, ‘w’ or ‘g’ then the program will decide if you win or loose or the match is a tie.
def gameWin(comp, you): if comp == you: return None elif comp == 's': if you == 'w': return False elif comp == 'g': return True elif comp == 'w': if you == 'g': return False elif comp == 's': return True elif comp == 'g': if you == 's': return False elif comp == 'w': return True ran = random.randint(1, 3) if ran == 1: comp = 's' elif ran == 2: comp = 'w' elif ran == 3: comp = 'g' you = input("Your turn: Snake(s) Water(w) or Gun (g)?\n") a = gameWin(comp, you) print(f"Computer chose {comp}") print(f"You chose {you}") if a == None: print("the game is a tie") elif a: print("You win") else: print("You lose")
Output:
