From 6ae74bde8be809e0ad84e1b1e35f2c8ce6ed65d9 Mon Sep 17 00:00:00 2001 From: grantslone Date: Wed, 3 Oct 2018 14:17:02 -0400 Subject: [PATCH 1/2] Added a simple Base Converter program. --- BaseCoverter.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 BaseCoverter.py diff --git a/BaseCoverter.py b/BaseCoverter.py new file mode 100644 index 0000000..b75e991 --- /dev/null +++ b/BaseCoverter.py @@ -0,0 +1,28 @@ +# Grant - grantslone + +def base_conv(num, frombase, tobase): + try: + frombase = int(frombase) + tobase = int(tobase) + num_dec = int(num, frombase) + b = [2,8,10,16] + for x in [frombase, tobase]: + if x not in b: + raise ValueError + except ValueError: + return "ValueError: Please use a valid number and/or base" + + base_dict = { + 2: ':b', + 8: ':o', + 10: ':d', + 16: ':x' + } + return "Converting " + str(num_dec) + " from Base(" + str(frombase) + ") to Base(" + str(tobase) + ") \nResult: "\ + + ('{' + base_dict[tobase] + '}').format(num_dec) + +num = input("Enter a number: ") +fbase = input("Please enter a base to start from [2, 8, 10, 16]: ") +tbase = input("Please enter a base you wish to convert to [2, 8, 10, 16]: ") + +print(base_conv(num, fbase, tbase)) \ No newline at end of file From 0331f4c011931eda2c381fec63cd176195393fc4 Mon Sep 17 00:00:00 2001 From: grantslone Date: Wed, 3 Oct 2018 14:22:41 -0400 Subject: [PATCH 2/2] Added README info --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9c8fb5..6e8467c 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Thanks! | [TicTacToe.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/TicTacToe.py) | 2 Player Tic Tac Toe | [Aakanksha](https://github.com/accakks) | | [RockPaperScissors.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/RockPaperScissors.py) | Rock Paper Scissors with Computer | [Aakanksha](https://github.com/accakks) | | [countCharacter.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/countCharacter.py) | Counts number of times each character occurs in the input string. | [Aakanksha](https://github.com/accakks) | - +| [BaseConverter.py](https://github.com/accakks/Simple-Programs-in-Python/blob/master/BaseConverter.py) | Converts an input number from one base to another. | [grantslone](https://github.com/grantslone) |