Oct-03-2019, 11:19 AM
Hi Christing,
I think we're all still a bit confused about your goal. So, I've just recreated the code with some comments that will help. Your code wouldn't run as shown above, so I've made enough changes to at least let it run through, and show the error as well.
I hope this helps! :)
I think we're all still a bit confused about your goal. So, I've just recreated the code with some comments that will help. Your code wouldn't run as shown above, so I've made enough changes to at least let it run through, and show the error as well.
I hope this helps! :)
try: mystring = "11111111111000000000000000000000" # TEST1 will include the string location of the first "1" # The first position on a string is always "0", the next is "1", the next is "2" # The string "mystring" goes form positions 0 -31 TEST1 = mystring.find('1')#POSITION # TEST1 now equals "0", the first position where it found a "1" print(TEST1) JUDGE = "1" #JUDGE if TEST1 == JUDGE: # TEST1 will NOT equal JUDGE (TEST1 = 0, JUDGE = 1) TEST1 = "OK" else: TEST1 = "NG" # TEST1 will now equal "NG". NOTE: This is a string and not a number now # A "try" statement must always include an "except" statement. except: pass # This tells Python to "do nothing" if the exception is hit #READ #=============================================================================== # all of the "TEST2 = mystring[:TEST1+X]" should definitely be a loop, for example... # for i in range(0,32): # Range is NOT INCLUSIVE. This will create a loop going from 0 to 31 (not 32) # TEST2 = mystring[:TEST1+i] #=============================================================================== ### Continuing with your code # This will error, because TEST1 was set to the string "NG" above # You cannot add a string to an integer ("NG" + 1 is an error) TEST2 = mystring[:TEST1+1] # This errors out # TEST2 = mystring[:TEST1+2] # .... snip ... TEST2 = mystring[:TEST1+32] # This errors out print (TEST2)The results this generates is...
0 Traceback (most recent call last): File "/home/username/eclipse-workspace/junkcode/testcode.py", line 31, in <module> TEST2 = mystring[:TEST1+1] # This errors out TypeError: must be str, not int