Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check for an integer in Python 3.2?
    primarykey
    data
    text
    <p>I'm trying to write a program where the user inputs a two-digit integer and the output is the second digit printed the amount of times indicated by the first digit. Here is what I have so far:</p> <pre><code>number = input('Type two-digit integer \n') a = int(number)//10 b = int(number)%10 if len(number) != 2: print(number, 'is not a two-digit integer') else: print(a*str(b)) </code></pre> <p>When I test this out it does what I intend it to do as long as someone types in numbers. If someone were to type in, say, 6r, an error message would pop up saying:</p> <p>a = int(number)//10</p> <p>ValueError: invalid literal for int() with base 10: '6r'</p> <p>So I would assume that something would need to be put in the second line of the code to test if the input is actually an integer, how would I do that? Would I be better off rewriting it in a different way? Please keep in mind that I'm taking an intro course to Python and this is a question on a practice midterm I am taking, so in the case that I would have to answer something like this on the real midterm I can't use a lot of complicated processes.</p> <p>This is something I tried that works if someone types something that isn't an integer, but for some reason that I don't know it gives the same message for non-integers to integers and doesn't function as I intend it to:</p> <pre><code>number = input('Type two-digit integer \n') if (isinstance(number, int)) == False: print(number, 'is not a two-digit integer') elif len(number) != 2: print(number, 'is not a two-digit integer') else: a = int(number)//10 b = int(number)%10 print(a*str(b)) </code></pre> <p>Help would be greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload