from __future__ import print_function import chalk as chk import random as rand import sys while True: try: class Welcome: print('Welcome to Coin Flip simulator!') toFlip = int(input('How many times would you like to flip the coin?\n')) class RandFlipGen: flipStor = [] def flipGen(self, toFlip): return self.flipStor.append(str(rand.randint(0,1))) class RepeatFlipGen: for i in range (0, Welcome.toFlip): RandFlipGen.flipGen(RandFlipGen, Welcome.toFlip) class PrintResult: flipStorConverted = '\n'.join(RandFlipGen.flipStor) headsOrTails = flipStorConverted.replace('0', 'Tails') result = headsOrTails.replace('1', 'Heads') d = dict.fromkeys(flipStorConverted, 0) for c in flipStorConverted: d[c] += 1 resultFormatted = '({} times Tails, {} times Heads)'.format(d['1'], d['0']) print('\n{}'.format(result)) print(resultFormatted) class SaveResults: toSave = str(input('Would you like to save the results?(Yes/No)')) if toSave.lower == 'yes' or toSave.lower() == 'y': file = open('results.txt', 'w') file.write('{}\n\n{}'.format(PrintResult.result, PrintResult.resultFormatted)) file.close() welcomeClass = Welcome() randomFlipGenClass = RandFlipGen() printResultClass = PrintResult() saveResultsClass = SaveResults() except ValueError as err: print(chk.red('\n{}\n'.format(err))) toEnd = str(input('Would you like to continue? (Yes/No)\n')) if toEnd.lower() == 'no' or toEnd.lower() == 'n': break
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question