Python - Check user input to make sure it is an integer - no letters, etc -
This code is a preview from the upcoming competition at picoctf.com. I've modified the original code so that it allows for user input, but I can not get Test # 2 to work. When a string of 10 characters is recorded with at least one letter, I have to print it that only numbers can be used. Currently it attempts to convert user input to an integer value to compare with the actual serial, but if a letter exists then the test fails because a letter can not be converted into an integer value:
123456789a Invalid verbatim "produce with base 10" ("int") "
How can I fix a piece of this code so that it can be properly tested ?
** < If the pre ( "test failed") >>
**
Your code is to find the serial number of your robot # to keep the solution simple - = 0933427186 Serial = input ("Please enter a valid serial number from your Robo Corporate Aircraft (Serial): = If print (' Only number allowed ')) Print (' Failed Test ') 2') Incorrect (Serial [0]) + ET (Serial [1]) = 9: Print ('Failed T False Return (Serial [4]) - Int (serial [2]) * Int (serial [3] ()) = 9: print ('unsuccessful test 4') return false return (serial [4]) - 5]) = 2: return (print failed 'test 5') if wrong (serial (serial [6]) / int (serial [7]) = 7: print ('unsuccessful wrong (serial [9]) - int (Serial [9]) = 2: print ('unsuccessful test 8') returned incorrect if Intel (serial [7]) * int (serial [1]] = 9: print ('unsuccessful test 9') return false if incorrect Direct (serial [2]) + int (serial [3]) = int (stream Hik [9]): Return ('failed test 10') Return return incorrect return correct if check_cierial (print): print ("Thanks! << Code>
use all the character numbers to test it:
& gt; & gt; 'a1'.isdigit () false & gt; & gt; & gt; 11'.isdigit () true & gt; & gt; 1a'.isdigit () Incorrect
You can be it your
serial string to make all of your trials easier, the sequence of integers In:
s_int = [int (d) for serial)]
then
if s_int [0 ] + S_int [1]! = 9:
etc.
You can easily create a sequence of tests with the index, an operator and expected result:
import operator test = ((0, 1, operator.ed, 9), (2, 3, operator.most, 9), (4, 5, operator. sub, 2), ( 5, 4, operated .mod, 2), (6, 7, operator. Rudy, 7), (8, 9, operator.), (7, 1, operator.mool, 9),) i, (a, b, op, rac ): Enamorate (TES) TS, 3): If the session (SIIT [A], SIIT [B]) = RIZ: print ("unsuccessful test {}" format (i) wrong return
Comments
Post a Comment