Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- coin = 0 # global variable,
- def SaveFile():
- global coin
- game_file = open('gamedata.txt', 'w') # w - write (override) , a - append
- game_file.write('\n'+str(coin)) # str - string - text
- game_file.close()
- def LoadFile():
- global coin
- game_file = open('gamedata.txt', 'r') # r - read only
- data = game_file.read().rstrip()
- coin = eval(data)
- game_file.close()
- Game()
- # Program Start Here
- def Menu():
- global coin
- coin = eval(input('how much coin?: '))
- while (1):
- print('0-Exit')
- print('1-Play Game')
- print('2-Save Game')
- print('3-Load saved Game')
- ans = input('please enter your choice: ')
- if ans == '0':
- exit()
- elif ans == '1':
- print('Playing Game now...')
- Game()
- elif ans == '2':
- print('Saving now....')
- SaveFile()
- elif ans == '3':
- print('Loading Game...')
- LoadFile()
- else:
- print('Wrong choice, only (0,1,2), please try again: ')
- def Game():
- global coin
- print(f'\n\nYou have {coin} coins')
- bet = eval(input('how much coin to bet: '))
- if bet > coin:
- bet = coin
- print('you cannot bet more than you have')
- print(f'your bet become: {bet}')
- input('press ENTER to roll the dice for PLAYER: ')
- player = random.randint(1, 6)
- print(f'your number is : {player}')
- input('press ENTER to roll the dice for COMPUTER: ')
- computer = random.randint(1, 6)
- print(f'computer number is : {computer}')
- if computer > player:
- coin = coin - bet
- print('Computer wins')
- print(f'Your coin is: {coin}')
- if coin == 0:
- ans1 = input('you lost everything! want to buy more coin: y\\n: ')
- if ans1 == 'y':
- coin = eval(input('how much coin?: '))
- else:
- print('No money: No more game! Bye bye')
- exit()
- elif player > computer:
- coin = coin + bet
- print('Player wins')
- print(f'Your coin is: {coin}')
- else:
- print('Draw')
- print(f'Your coin is: {coin}')
- # Program Start Here
- Menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement