Advertisement
DrAungWinHtut

dice5.py

May 9th, 2023
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. import os
  2. import random
  3.  
  4. coin = 0
  5.  
  6. def LoadGame(fname):
  7.     global coin
  8.     if(os.path.exists(fname)):
  9.         print('\nLoading.....')
  10.         load_file = open(fname,"r")
  11.         coin = int(load_file.read())
  12.         load_file.close()
  13.         print('Saved game loaded...\n\n')
  14.         print(f'your coin is now {coin}\n\n')
  15.         Game()
  16.     else:
  17.         print(f'\nNo saved file for user {fname}\n\n')
  18.  
  19. def GaveSave(fname):
  20.     global coin
  21.     game_file = open(fname,"w")
  22.     game_file.write(str(coin))
  23.     game_file.close()
  24.     print("\nGame is Saved Successfully!\n\n")
  25.  
  26.  
  27.  
  28. def Game():
  29.     global coin
  30.     bet = eval(input('how much will you bet? : '))
  31.    
  32.     if bet > coin:
  33.         print('\nNo... you cannot bet that much')
  34.         print(f'you only have {coin}')
  35.         print(f'your bet become {coin}')
  36.         bet = coin
  37.     input('\npress any key to roll..')
  38.     me = random.randint(1,6)
  39.     print(f'your number is {me}')
  40.    
  41.     print('\ncomputer turn')
  42.     comp = random.randint(1,6)
  43.     print(f'computer number is {comp}')
  44.     print()
  45.     if(comp>me):
  46.         print('computer wins')
  47.         coin = coin - bet
  48.     elif(comp<me):
  49.         print('you win')
  50.         coin = coin + bet
  51.     else:
  52.         print('draw')
  53.     print(f'\nyour coin is {coin}')
  54.     if coin == 0:
  55.         print('\nyou need to buy more coin')
  56.         print('will you buy or exit?')
  57.         ans = input('y/n? ')
  58.         if ans == 'y':
  59.             coin = int(input('\nhow many coin: '))
  60.         else:
  61.             print('\n\nSee you next time buddy!')
  62.             exit()
  63.    
  64.  
  65.  
  66.  
  67.  
  68.  
  69. # Program start here
  70. os.system('clear') # for windows use cls
  71. name = input('enter your name: ')
  72. # Aung Win Htut -> aung_win_htut
  73. print(name)
  74. name = name.lower()
  75. print(name)
  76. name = name.replace(" ","_")
  77. print(name)
  78. file_name = name + ".txt"
  79.  
  80.  
  81.  
  82.  
  83. coin = int(input('how many coin? :'))
  84.  
  85. while(True):
  86.     print('1 - Play Game')
  87.     print('2 - Save Game')
  88.     print('3 - Load Game')
  89.     print('0 - Exit')
  90.     ans = input('0, 1, 2 or 3 ? : ')
  91.     if(ans=='1'):
  92.         print('\nPlaying Game...')
  93.         Game()
  94.         print()
  95.     elif(ans=='2'):
  96.         GaveSave(file_name)
  97.     elif(ans=='3'):
  98.         LoadGame(file_name)
  99.     elif(ans=='0'):
  100.         print('Exiting now...')
  101.         print()
  102.         exit()
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement