From a book that programmers can learn: Converting characters that represent numbers to integer types

From the continuation of Last time

Problem: Knowing that you have read the numbers to the end I will try to make it a smaller problem to solve this Click here for books ["Books that programmers can learn in an interesting way. Let's train problem-solving skills! ]](Http://www.amazon.co.jp/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%E3%81% AE% E8% 80% 83% E3% 81% 88% E6% 96% B9% E3% 81% 8C% E3% 81% 8A% E3% 82% 82% E3% 81% 97% E3% 82% 8D% E3% 81% 84% E3% 81% BB% E3% 81% A9% E8% BA% AB% E3% 81% AB% E3% 81% A4% E3% 81% 8F% E6% 9C% AC-% E5 % 95% 8F% E9% A1% 8C% E8% A7% A3% E6% B1% BA% E8% 83% BD% E5% 8A% 9B% E3% 82% 92% E9% 8D% 9B% E3% 81 % 88% E3% 82% 88% E3% 81% 86% EF% BC% 81-% E3% 82% A2% E3% 82% B9% E3% 82% AD% E3% 83% BC% E6% 9B% B8% E7% B1% 8D-% EF% BC% B6% EF% BC% 8E% EF% BC% A1% EF% BD% 8E% EF% BD% 94% EF% BD% 8F% EF% BD% 8E -% EF% BC% B3% EF% BD% 90% EF% BD% 92% EF% BD% 81% EF% BD% 95% EF% BD% 8C-ebook / dp / B00MB2STXK? Ie = UTF8 & keywords =% E3 % 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9E & qid = 1460663927 & ref_ = sr_1_5 & sr = 8-5)

Problem: Convert numbers to convert to integer type > Write a program that receives the characters that represent the numbers 0-9 that the user enters. The received character is converted to the same number (range from 0 to 9), and the converted number is displayed as a result.

With this kind of content, in the example answer, subtraction was performed using the fact that there is always a difference of 48 between the character string numerical value and the integer value of the ASCII character code. (In the case of character string 0, "7" is the integer value 7 that you want to find with the character code 55, so the difference is 48)

C ++ depicted as this answer

char digit;
cout << "Enter a one-digit number: ";
cin >> digit;
int sum = digit - '0';
cout << "Is the sum of digits " << sum << "? \n";

Your answer
#!/usr/bin/env python
#coding:utf-8

###def number(x):
###    print("Enter a one-digit number:",x)
###    digit = ord(x) 
###    sum = digit - ord('0')
###    print("Is the sum of digits:",sum)

###・ ・ ・ ・(Execution result on the terminal)
###>>> from ascii import number
###>>> number(str(7))
###Enter a one-digit number: 7
###Is the sum of digits: 7

(Hereafter, the correction code is described)//Thank you for the edit code
from ConsoleOut import cout

def number():
    cout << "Enter a one-digit number: "
    digit = input()
    value = int(digit)
    cout << "The numerical value of the digit is:"+str(valule)+"\n"

・ ・ ・ ・ (On the terminal)
>>> number()
Enter a one-digit number: 7
The numerical value of the digit is:7


I feel that I learned a little about ASCII character encoding in this problem. But what other scenes do you use this ASCII character code for? Unicode and ASCII code I copied and pasted without thinking too much, so it was an opportunity to think about it.

Recommended Posts

From a book that programmers can learn: Converting characters that represent numbers to integer types
From a book that programmers can learn ... (Python): Pointer
From a book that programmers can learn ... (Python): About sorting
From a book that programmers can learn (Python): Decoding messages
From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... (Python): Review of arrays
From a book that programmers can learn ... Collect small problem parts
From a book that programmers can learn (Python): Statistical processing-deviation value
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
8 services that even beginners can learn Python (from beginners to advanced users)
From a book that makes the programmer's way of thinking interesting (Python)
A mechanism to call a Ruby method from Python that can be done in 200 lines
"A book that understands Flask from scratch" Reading memo
Convert mesh data exported from SpriteUV2 to a format that can be imported by Spine