<small id='QbfE7'></small><noframes id='QbfE7'>

    1. <tfoot id='QbfE7'></tfoot>
        <bdo id='QbfE7'></bdo><ul id='QbfE7'></ul>
      <i id='QbfE7'><tr id='QbfE7'><dt id='QbfE7'><q id='QbfE7'><span id='QbfE7'><b id='QbfE7'><form id='QbfE7'><ins id='QbfE7'></ins><ul id='QbfE7'></ul><sub id='QbfE7'></sub></form><legend id='QbfE7'></legend><bdo id='QbfE7'><pre id='QbfE7'><center id='QbfE7'></center></pre></bdo></b><th id='QbfE7'></th></span></q></dt></tr></i><div id='QbfE7'><tfoot id='QbfE7'></tfoot><dl id='QbfE7'><fieldset id='QbfE7'></fieldset></dl></div>
    2. <legend id='QbfE7'><style id='QbfE7'><dir id='QbfE7'><q id='QbfE7'></q></dir></style></legend>
      1. 将整数拆分为数字以计算 ISBN 校验和

        时间:2023-09-11

          • <bdo id='Io19r'></bdo><ul id='Io19r'></ul>
            1. <small id='Io19r'></small><noframes id='Io19r'>

                <tfoot id='Io19r'></tfoot>

                  <i id='Io19r'><tr id='Io19r'><dt id='Io19r'><q id='Io19r'><span id='Io19r'><b id='Io19r'><form id='Io19r'><ins id='Io19r'></ins><ul id='Io19r'></ul><sub id='Io19r'></sub></form><legend id='Io19r'></legend><bdo id='Io19r'><pre id='Io19r'><center id='Io19r'></center></pre></bdo></b><th id='Io19r'></th></span></q></dt></tr></i><div id='Io19r'><tfoot id='Io19r'></tfoot><dl id='Io19r'><fieldset id='Io19r'></fieldset></dl></div>
                  <legend id='Io19r'><style id='Io19r'><dir id='Io19r'><q id='Io19r'></q></dir></style></legend>
                    <tbody id='Io19r'></tbody>
                • 本文介绍了将整数拆分为数字以计算 ISBN 校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在编写一个计算 ISBN 号校验位的程序.我必须将用户的输入(ISBN 的九位数字)读入一个整数变量,然后将最后一位数字乘以 2,倒数第二位乘以 3,依此类推.我怎样才能将整数拆分"成它的组成数字来做到这一点?由于这是一项基本的家庭作业,我不应该使用列表.

                  I'm writing a program which calculates the check digit of an ISBN number. I have to read the user's input (nine digits of an ISBN) into an integer variable, and then multiply the last digit by 2, the second last digit by 3 and so on. How can I "split" the integer into its constituent digits to do this? As this is a basic homework exercise I am not supposed to use a list.

                  推荐答案

                  只需创建一个字符串.

                  myinteger = 212345
                  number_string = str(myinteger)
                  

                  够了.现在您可以对其进行迭代:

                  That's enough. Now you can iterate over it:

                  for ch in number_string:
                      print ch # will print each digit in order
                  

                  或者你可以切片:

                  print number_string[:2] # first two digits
                  print number_string[-3:] # last three digits
                  print number_string[3] # forth digit
                  

                  <小时>

                  或者更好的是,不要将用户的输入转换为整数(用户键入字符串)


                  Or better, don't convert the user's input to an integer (the user types a string)

                  isbn = raw_input()
                  for pos, ch in enumerate(reversed(isbn)):
                      print "%d * %d is %d" % pos + 2, int(ch), int(ch) * (pos + 2)
                  

                  有关更多信息,请阅读教程.

                  For more information read a tutorial.

                  这篇关于将整数拆分为数字以计算 ISBN 校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:pandas :减去两个日期列,结果是一个整数 下一篇:“OverflowError: Python int too large to convert to C long&q

                  相关文章

                  最新文章

                    <tfoot id='TzyC7'></tfoot>
                  1. <legend id='TzyC7'><style id='TzyC7'><dir id='TzyC7'><q id='TzyC7'></q></dir></style></legend>

                      <i id='TzyC7'><tr id='TzyC7'><dt id='TzyC7'><q id='TzyC7'><span id='TzyC7'><b id='TzyC7'><form id='TzyC7'><ins id='TzyC7'></ins><ul id='TzyC7'></ul><sub id='TzyC7'></sub></form><legend id='TzyC7'></legend><bdo id='TzyC7'><pre id='TzyC7'><center id='TzyC7'></center></pre></bdo></b><th id='TzyC7'></th></span></q></dt></tr></i><div id='TzyC7'><tfoot id='TzyC7'></tfoot><dl id='TzyC7'><fieldset id='TzyC7'></fieldset></dl></div>
                      • <bdo id='TzyC7'></bdo><ul id='TzyC7'></ul>

                    1. <small id='TzyC7'></small><noframes id='TzyC7'>