This code is a preview from an upcoming competition at picoctf.com. I modified the original code provided so it allows for user input, but I can't get Test #2 to work. When a string of 10 characters is entered with at least one letter, I want it to print that only numbers may be used. Currently it tries to convert the user input to an integer value to compare it to the actual serial, but if there is a letter present the test fails since a letter can't be converted to a integer value:
123456789a yields "invalid literal for int() with base 10..."
How can I fix this piece of code so that it tests correctly?
**
if int(serial) != serial:
print ("only numbers allowed")
print ('Failed Test 2')
**
#!/usr/bin/env python
# Your goal is to find the input that will verify your robot's serial number
#solution to keep it handy = 0933427186
serial = input ("Please enter a valid serial number from your RoboCorpIntergalactic purchase")
def check_serial(serial):
if len(serial) != 10:
print ('Failed Test 1')
return False
if int(serial) != serial:
print ("only numbers allowed")
print ('Failed Test 2')
return False
if int(serial[0])+int(serial[1]) != 9:
print ('Failed Test 3')
return False
if int(serial[2])*int(serial[3]) != 9:
print ('Failed Test 4')
return False
if int(serial[4])-int(serial[5]) != 2:
print ('Failed Test 5')
return False
if int(serial[5])%int(serial[4]) != 2:
print ('Failed Test 6')
return False
if int(serial[6])/int(serial[7]) != 7:
print ('Failed Test 7')
return False
if int(serial[8])-int(serial[9]) != 2:
print ('Failed Test 8')
return False
if int(serial[7])*int(serial[1]) != 9:
print ('Failed Test 9')
return False
if int(serial[2]) + int(serial[3]) != int(serial[9]):
print ('Failed Test 10')
return False
return True
if check_serial(serial):
print ("Thank you! Your product has been verified!")
else:
print ("I'm sorry that is incorrect. Please use a valid RoboCorpIntergalactic serial number")
int(somestring) == somestringwill always be false, even if it is all digits.