In: Computer Science
(PYTHON)
prompt the user to enter a single-digit num and print that num as follow:
1) use loops
2) print num in words. Example: 5 is 'five'
3) the number of spaces that go back and forth should equal the num
if the user inputs 5, the program prints:
five
. . . . .five
five
. . . . . five
five
and 2 would be:
two
. .two
(the periods(.) are only for demonstration purposes. Don't include it in the actual output).
program with explanation (Python 3.7)
# python dictionary used to store
key: value pair.
# Dictionaries are optimized to retrieve values when the key is
known.
num2words = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five',
6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine'}
# function definition part
def number(Number):
# if condition: used to check number between 0 and 10
if (Number >= 1) and (Number <=9):
# Using the key retrieves the value from the dictionary
value= num2words[Number]
# Loop part : printing the number in its number of times
for x in range(Number):
print(value)
else:
# else part of the loop
print("Number Out Of Range")
# calls main() for continue this problem
main()
# main part part of the
program
# when program runs it start from here
def main():
# Takes input from the user
num = eval(input("Please enter a number between 0 and 10: "))
# function calling part and pass value num
number(num)
# calling main() to start program
main()
code only(Python 3.7)
num2words = {1: 'One', 2: 'Two', 3:
'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9:
'Nine'}
def number(Number):
if (Number >= 1) and (Number <=9):
value= num2words[Number]
for x in range(Number):
print(value)
else:
print("Number Out Of Range")
main()
def main():
num = eval(input("Please enter a number between 0 and 10: "))
number(num)
main()
output