Advertisement
Divyansh_Chourey

Snake_water_gun

Jan 15th, 2021 (edited)
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. '''
  2. Snake Water Gun Game
  3. Source code:Divyansh Chourey
  4. '''
  5.  
  6. import random
  7.  
  8. run=True
  9.  
  10. #Function for declaring result
  11. def game():
  12.     """docstring for game"""
  13.     if user=='s':
  14.         if comp=='s':
  15.             return 'tie'
  16.         elif comp=='w':
  17.             return 'User won'
  18.         elif comp=='g':
  19.             return 'User lost'
  20.     elif user=='w':
  21.         if comp=='s':
  22.             return 'User lost'
  23.         elif comp=='w':
  24.             return 'tie'
  25.         elif comp=='g':
  26.             return 'User won'
  27.     elif user=='g':
  28.         if comp=='s':
  29.             return 'User won'
  30.         elif comp=='w':
  31.             return 'User lost'
  32.         elif comp=='g':
  33.             return 'tie'
  34.     # TODO: write code...
  35.    
  36. display='Welcome to SWG\nYou have the follwing options:\n\tSnake(s)\n\tWater(w)\n\tGun(g)'
  37. print(display)
  38.  
  39. #Using while loop to run the game
  40. while run:
  41.     #Taking the input from user
  42.     user=input("\nEnter: ")
  43.     #Generating random number
  44.     rand_num=random.randint(1,3)
  45.     #Converting the random digit to a variable
  46.     if rand_num==1:
  47.         comp="s"
  48.     elif rand_num==2:
  49.         comp="w"
  50.     elif rand_num==3:
  51.         comp="g"
  52.     #Exit from the game
  53.     if user=='quit':
  54.         print("You quit the game")
  55.         break
  56.     else:
  57.         run = True
  58.    
  59.     result=game()
  60.    
  61.     #displaying the final results
  62.     if result=='User won':
  63.         result="You won\n"
  64.     elif result=='User lost':
  65.         result="You lose\n"
  66.     elif result=='tie':
  67.         result="TIE!\n"
  68.        
  69.     dis_result='\nComputer chose: '+comp+'\nYou chose: '+user+'\nResult: '+result
  70.     print(dis_result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement