ssoni

basketball.py

Nov 1st, 2026 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import time
  2. import random
  3.  
  4. def main():
  5.     ptsA = 0
  6.     ptsB = 0
  7.     gameClock = 15*60
  8.     whoHasBall = 'a'
  9.  
  10.     while (gameClock > 0):
  11.         #distance = int(input('How far from basket? '))
  12.         distance = random.randint(3,30)
  13.         print(f'Team {whoHasBall} shoots the ball from {distance} feet!')
  14.         result = shoot(distance)
  15.         if result > 0:
  16.             if whoHasBall == 'a':
  17.                 ptsA += result
  18.             else:
  19.                 ptsB += result
  20.             print(f'Score!  Score is now {ptsA}-{ptsB}.')
  21.             if whoHasBall == 'a':
  22.                 whoHasBall = 'b'
  23.             else:
  24.                 whoHasBall = 'a'
  25.  
  26.         else:
  27.             print('Miss!')
  28.        
  29.             rebound = random.randint(1,4)
  30.             if rebound == 1:
  31.                 print(f'Team {whoHasBall} keeps the ball!')
  32.             else:
  33.                 if whoHasBall == 'a':
  34.                     whoHasBall = 'b'
  35.                 else:
  36.                     whoHasBall = 'a'
  37.  
  38.         gameClock = gameClock - random.randint(10,24)
  39.         print(f'{gameClock} seconds left on the clock')
  40.         print()
  41.         time.sleep(.1)
  42.  
  43.     print('Game over')
  44.     print(f'Final score is {ptsA}-{ptsB}.')
  45.  
  46. def shoot(d):
  47.     pct = (90*.95**d)/100
  48.     #print(d, pct)
  49.     num = random.random()
  50.     if num < pct:
  51.         if d>24:
  52.             return 3
  53.         else:
  54.             return 2
  55.     else:
  56.         return 0
  57.  
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment