Getting started with programming in Python Continued from Last time
Also, this time, Problem presentation before that ・ Know that you have read the numbers to the end I will challenge the problem to solve this.
However, as always, it's difficult to keep going.
char digit;
int checksum = 0;
cout << "Enter a six-digit number:";
for (int position = 1; position <= 6; position ++) {
cin >> digit;
checksum += digit - '0';
}
cout << "Checksum is " << checksum << ". \n";
if (checksum % 10 == 0) {
cout << "Checksum is divisible by 10. Valid. \n";
} else {
cout << "Checksum is not divisible by 10. Invalid. \n";
}
ConsoleOut.py
#!/usr/bin/env python
#coding:utf-8
#Using cout in python taught by shiracamus
from ConsoleOut import cout
def number():
checksum = 0
for i in range(6):
cout << "Enter a six-digit number:"
digit = input()
checksum += int(digit)
cout << "Checksum is " + str(checksum) + ". \n";
if checksum % 10 == 0:
cout << "Checksum is divisible by 10. Valid .\n"
else:
cout << "Checksum is not divisible by 10. Invalid. \n"
test01.py(Add 6-digit input numbers)
###I wrote it though it is not so much that I can say that I made it myself.
#!/usr/bin/env python
#coding:utf-8
from ConsoleOut import cout
def number():
checksum = 0
for i in range(6):
cout << "Enter a six-digit number:"
digit = input()
checksum += int(digit)
cout << "Checksum is " + str(checksum) + ". \n";
if checksum % 10 == 0:
cout << "Checksum is divisible by 10. Valid .\n"
else:
cout << "Checksum is not divisible by 10. Invalid. \n"
Most of the parts are written by diversion so far, The understanding of int type, str type, internal variables, etc. is still insufficient, and the first checksum + = int (digit) For some reason, I was worried that they wouldn't add up. Declared with checksum = 0? If you do, they will add up. I wonder if I could do it for the time being (sweat)
test.py(Function to add each digit if the number of inputs is 10 or more)
#!/usr/bin/env python
#coding:utf-8
def doubleDigitValue(digit):
doubledDigit = digit * 2
if (doubledDigit >= 10):
sum = 1 + doubledDigit % 10
else:
sum = doubledDigit
return sum
And here is the previous one
test.py(Added doubleDigitValue function)
#!/usr/bin/env python
#coding:utf-8
from ConsoleOut import cout
from test import doubleDigitValue
def number():
checksum = 0
for i in range(6):
cout << "Enter a six-digit number:"
digit = input()
if i % 2 == 0:
checksum += int(digit)
else: #If it is an odd number, double it and if it is 10 or more, add each digit.
checksum += doubleDigitValue(int(digit))
cout << "Checksum is " + str(checksum) + ". \n";
if checksum % 10 == 0:
cout << "Checksum is divisible by 10. Valid .\n"
else:
cout << "Checksum is not divisible by 10. Invalid. \n"
Execution result
>>> from test01 import number
>>> number()
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Checksum is 9.
Checksum is not divisible by 10. Invalid.
>>> number()
Enter a six-digit number:2 #2
Enter a six-digit number:1 #1*2
Enter a six-digit number:1 #1
Enter a six-digit number:1 #1*2
Enter a six-digit number:1 #1
Enter a six-digit number:1 #1*2
Checksum is 10.
Checksum is divisible by 10. Valid .
>>>
#The problem has been solved. Thank you!
As a first result (1 * 2) + 1 + (1 * 2) + 1 + (1 * 2) +1 = 9 // 1st time When the result is displayed, it seems to be correct at first I think it's different to enter it the second time. (2 * 2) + 1 + (1 * 2) + 1 + (1 * 2) +1 = 10 ??? // 2nd time It's an inadequate situation, but let's think a little more.
Recommended Posts