Advertisement
DrAungWinHtut

game_finish.py

May 21st, 2023
1,256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. import random
  2.  
  3. coin = 0  # global variable,
  4.  
  5.  
  6. def SaveFile():
  7.     global coin
  8.     game_file = open('gamedata.txt', 'w')  # w - write (override) , a - append
  9.     game_file.write('\n'+str(coin))  # str - string - text
  10.     game_file.close()
  11.  
  12.  
  13. def LoadFile():
  14.     global coin
  15.     game_file = open('gamedata.txt', 'r')  # r - read only
  16.     data = game_file.read().rstrip()
  17.     coin = eval(data)
  18.     game_file.close()
  19.     Game()
  20. # Program Start Here
  21.  
  22.  
  23. def Menu():
  24.     global coin
  25.     coin = eval(input('how much coin?: '))
  26.     while (1):
  27.         print('0-Exit')
  28.         print('1-Play Game')
  29.         print('2-Save Game')
  30.         print('3-Load saved Game')
  31.         ans = input('please enter your choice: ')
  32.         if ans == '0':
  33.             exit()
  34.         elif ans == '1':
  35.             print('Playing Game now...')
  36.             Game()
  37.         elif ans == '2':
  38.             print('Saving now....')
  39.             SaveFile()
  40.         elif ans == '3':
  41.             print('Loading Game...')
  42.             LoadFile()
  43.         else:
  44.             print('Wrong choice, only (0,1,2), please try again: ')
  45.  
  46.  
  47. def Game():
  48.     global coin
  49.     print(f'\n\nYou have {coin} coins')
  50.     bet = eval(input('how much coin to bet: '))
  51.  
  52.     if bet > coin:
  53.         bet = coin
  54.         print('you cannot bet more than you have')
  55.         print(f'your bet become: {bet}')
  56.  
  57.     input('press ENTER to roll the dice for PLAYER: ')
  58.     player = random.randint(1, 6)
  59.     print(f'your number is : {player}')
  60.  
  61.     input('press ENTER to roll the dice for COMPUTER: ')
  62.     computer = random.randint(1, 6)
  63.     print(f'computer number is : {computer}')
  64.  
  65.     if computer > player:
  66.         coin = coin - bet
  67.         print('Computer wins')
  68.         print(f'Your coin is: {coin}')
  69.         if coin == 0:
  70.             ans1 = input('you lost everything! want to buy more coin: y\\n: ')
  71.             if ans1 == 'y':
  72.                 coin = eval(input('how much coin?: '))
  73.             else:
  74.                 print('No money: No more game! Bye bye')
  75.                 exit()
  76.  
  77.     elif player > computer:
  78.         coin = coin + bet
  79.         print('Player wins')
  80.         print(f'Your coin is: {coin}')
  81.  
  82.     else:
  83.         print('Draw')
  84.         print(f'Your coin is: {coin}')
  85.  
  86.  
  87. # Program Start Here
  88. Menu()
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement