|
| 1 | +# Lab 03 Numbers as words man |
| 2 | + |
| 3 | + |
| 4 | +# Muki - Lab03 Numbers to Phrases |
| 5 | + |
| 6 | + |
| 7 | +''' |
| 8 | +Lab 3: Number to Phrase |
| 9 | +Convert a given number into its english representation. For example: 67 becomes 'sixty-seven'. Handle numbers from 0-99. |
| 10 | +
|
| 11 | +Hint: you can use modulus to extract the ones and tens digit. |
| 12 | +
|
| 13 | +x = 67 |
| 14 | +tens_digit = x//10 |
| 15 | +ones_digit = x%10 |
| 16 | +Hint 2: use the digit as an index for a list of strings OR as a key for a dict of digit:phrase pairs. |
| 17 | +
|
| 18 | +Version 2 |
| 19 | +Handle numbers from 100-999. |
| 20 | +
|
| 21 | +Version 3 (optional) |
| 22 | +Convert a number to roman numerals. |
| 23 | +
|
| 24 | +Version 4 (optional) |
| 25 | +Convert a time given in hours and minutes to a phrase. |
| 26 | +
|
| 27 | +This block was borrowed from class |
| 28 | +''' |
| 29 | +# imports just in case, I will remove these if I don't use them |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +# Version 1: |
| 34 | +numbah = input('please enter a number between 0 and 999:\n> ') |
| 35 | + |
| 36 | +hundreds_place = int(numbah)//100 |
| 37 | +lose = int(numbah) - (hundreds_place * 100) |
| 38 | +ones_place = int(lose)%10 |
| 39 | +tens_place = int(lose)//10 |
| 40 | +# print(lose) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +print(f'You entered:{numbah}\t Thanks for your entry.') |
| 45 | +# print(ones_place) |
| 46 | +# print(tens_place) |
| 47 | +# print(hundreds_place) |
| 48 | +# print(lose) |
| 49 | + |
| 50 | + |
| 51 | +single_digits = { |
| 52 | + '0':'', |
| 53 | + '1':'one', |
| 54 | + '2':'two', |
| 55 | + '3':'three', |
| 56 | + '4':'four', |
| 57 | + '5':'five', |
| 58 | + '6':'six', |
| 59 | + '7':'seven', |
| 60 | + '8':'eight', |
| 61 | + '9':'nine', |
| 62 | +} |
| 63 | +tens_digits = { |
| 64 | + '0':'', |
| 65 | + '1':'', |
| 66 | + '2':'twenty-', |
| 67 | + '3':'thirty-', |
| 68 | + '4':'fourty-', |
| 69 | + '5':'fifty-', |
| 70 | + '6':'sixty-', |
| 71 | + '7':'seventy-', |
| 72 | + '8':'eighty-', |
| 73 | + '9':'ninety-', |
| 74 | +} |
| 75 | + |
| 76 | +special_cases = { |
| 77 | + '10':'ten', |
| 78 | + '11':'eleven', |
| 79 | + '12':'twelve', |
| 80 | + '13':'thirteen', |
| 81 | + '14':'fourteen', |
| 82 | + '15':'fifteen', |
| 83 | + '16':'sixteen', |
| 84 | + '17':'seventeen', |
| 85 | + '18':'eighteen', |
| 86 | + '19':'nineteen', |
| 87 | + |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +hundreds_digits = { |
| 92 | + '0':'', |
| 93 | + '1':'one hundred', |
| 94 | + '2':'two hundred', |
| 95 | + '3':'three hundred', |
| 96 | + '4':'four hundred', |
| 97 | + '5':'five hundred', |
| 98 | + '6':'six hundred', |
| 99 | + '7':'seven hundred', |
| 100 | + '8':'eight hundred', |
| 101 | + '9':'nine hundred', |
| 102 | +} |
| 103 | + |
| 104 | +# if numbah: |
| 105 | +if numbah == "0": |
| 106 | + print(f'{numbah} is: zero') |
| 107 | +elif hundreds_place == 0: |
| 108 | + if tens_place != 1: |
| 109 | + print(f'{numbah} is: {tens_digits[str(tens_place)]}{single_digits[str(ones_place)]}') |
| 110 | + if numbah in special_cases: |
| 111 | + print(f'{numbah} is: {special_cases[numbah]}') |
| 112 | +elif hundreds_place >= 1: |
| 113 | + if str(lose) in special_cases: |
| 114 | + print(f'{numbah} is: {hundreds_digits[str(hundreds_place)]} {special_cases[str(lose)]}') |
| 115 | + elif tens_place == 0 and hundreds_place >= 1: |
| 116 | + print(f'{numbah} is: {hundreds_digits[str(hundreds_place)]} and {single_digits[str(ones_place)]}') |
| 117 | + elif tens_place != 1: |
| 118 | + print(f'{numbah} is: {hundreds_digits[str(hundreds_place)]} {tens_digits[str(tens_place)]}{single_digits[str(ones_place)]}') |
| 119 | + elif str(lose) not in special_cases: |
| 120 | + print(f'{numbah} is: {hundreds_digits[str(hundreds_place)]} {tens_digits[str(tens_place)]}{single_digits[str(ones_place)]}') |
0 commit comments