Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a program that accepts a two digit # that breaks it down

Tags:

python

I am currently using Python to create a program that accepts user input for a two digit number and will output the numbers on a single line.

For Example: My program will get a number from the user, lets just use 27

I want my program to be able to print "The first digit is 2" and "The second digit is 7" I know I will have to use modules (%) but I am new to this and a little confused!

like image 733
user2197867 Avatar asked Oct 21 '22 14:10

user2197867


2 Answers

Try this:

val = raw_input("Type your number please: ")
for i, x in enumerate(val, 1):
    print "#{0} digit is {1}".format(i, x)
like image 137
Artsiom Rudzenka Avatar answered Oct 24 '22 04:10

Artsiom Rudzenka


It was not clear from your question whether you are looking to use % for string substitution, or % for remainder.

For completeness, the mathsy way using modulus operator on ints would look like this:

>>> val = None
>>> while val is None:
...   try:
...     val = int(raw_input("Type your number please: "))
...   except ValueError:
...     pass
... 
Type your number please: potato
Type your number please: 27
>>> print 'The first digit is {}'.format(val // 10)
The first digit is 2
>>> print 'The second digit is {}'.format(val % 10)
The second digit is 7
like image 21
wim Avatar answered Oct 24 '22 05:10

wim