Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How i can judge my code
#16
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! :)

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
Reply


Messages In This Thread
How i can judge my code - by christing - Oct-02-2019, 04:16 AM
RE: How i can judge my code - by buran - Oct-02-2019, 06:02 AM
RE: How i can judge my code - by christing - Oct-02-2019, 09:20 AM
RE: How i can judge my code - by CoderMan - Oct-02-2019, 09:24 AM
RE: How i can judge my code - by christing - Oct-02-2019, 12:32 PM
RE: How i can judge my code - by SheeppOSU - Oct-02-2019, 12:39 PM
RE: How i can judge my code - by christing - Oct-02-2019, 01:53 PM
RE: How i can judge my code - by ichabod801 - Oct-02-2019, 03:16 PM
RE: How i can judge my code - by christing - Oct-02-2019, 04:11 PM
RE: How i can judge my code - by jefsummers - Oct-02-2019, 04:48 PM
RE: How i can judge my code - by christing - Oct-02-2019, 05:50 PM
RE: How i can judge my code - by newbieAuggie2019 - Oct-02-2019, 06:33 PM
RE: How i can judge my code - by christing - Oct-03-2019, 09:38 AM
RE: How i can judge my code - by newbieAuggie2019 - Oct-03-2019, 02:23 PM
RE: How i can judge my code - by buran - Oct-02-2019, 06:46 PM
RE: How i can judge my code - by christing - Oct-03-2019, 12:33 AM
RE: How i can judge my code - by burningkrome - Oct-03-2019, 11:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i judge 1st string position is correct number christing 3 2,534 Oct-30-2019, 03:32 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020