Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How i can judge my code
#1
Below is the code i write for confirm my string single position.

the purpose i write the code is i want single confirm my string(11111111111000000000000000000000) every position if the string position is one system will reply 1 or OK else if the string is 0 system will reply O OR NG


try:
 mystring = "11111111111000000000000000000000"
 TEST1 = mystring.find('1')#POSITION
 JUDGE = "1"


#JUDGE
 if TEST1 == JUDGE:
  TEST1  = "OK"
 else:
  TEST1 = "NG"
  
#READ 

 TEST2 = strTargetData[:TEST1+1]
 TEST2 = strTargetData[:TEST1+2]
 TEST2 = strTargetData[:TEST1+3]
 TEST2 = strTargetData[:TEST1+4]
 TEST2 = strTargetData[:TEST1+5]
 TEST2 = strTargetData[:TEST1+6]
 TEST2 = strTargetData[:TEST1+7]
 TEST2 = strTargetData[:TEST1+8]
 TEST2 = strTargetData[:TEST1+9]
 TEST2 = strTargetData[:TEST1+10]
 TEST2 = strTargetData[:TEST1+11]
 TEST2 = strTargetData[:TEST1+12]
 TEST2 = strTargetData[:TEST1+13]
 TEST2 = strTargetData[:TEST1+14]
 TEST2 = strTargetData[:TEST1+15]
 TEST2 = strTargetData[:TEST1+16]
 TEST2 = strTargetData[:TEST1+17]
 TEST2 = strTargetData[:TEST1+18]
 TEST2 = strTargetData[:TEST1+19]
 TEST2 = strTargetData[:TEST1+20]
 TEST2 = strTargetData[:TEST1+21]
 TEST2 = strTargetData[:TEST1+22]
 TEST2 = strTargetData[:TEST1+23]
 TEST2 = strTargetData[:TEST1+24]
 TEST2 = strTargetData[:TEST1+25]
 TEST2 = strTargetData[:TEST1+26]
 TEST2 = strTargetData[:TEST1+27]
 TEST2 = strTargetData[:TEST1+28]
 TEST2 = strTargetData[:TEST1+29]
 TEST2 = strTargetData[:TEST1+30]
 TEST2 = strTargetData[:TEST1+31]
 TEST2 = strTargetData[:TEST1+32]
 

print (TEST2)


my output is 11111111111000000000000000000000
Reply
#2
I don't know about other members, but for me it's not clear what you try to do. Also, what strTargetData is? What does "judge my code/string" means?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-02-2019, 06:02 AM)buran Wrote: I don't know about other members, but for me it's not clear what you try to do. Also, what strTargetData is? What does "judge my code/string" means?
strTargetData is i write wrong info actaully i want to write mystring not strTargetData
Good Day,

the question I would like to ask is I can judge in (mystring = "1111111111100000000000") every single position. and want use 1 to judge.

for example

myjudge = "1"
mystring = "1111111111100000000000"
mystring[1] #this is the code i judge my string position 1

if mystring == "myjudge":
print mystring = 1
else:
print mystring = 0
my output will get 1 if the judgment is correct else will judge 0

actually I want to write the code on the simple way but I am no ideal how can I write my code on complex way I total write 32 times to judge (mystring=1111111111100000000000) result


my output result is
1111111111100000000000



mystring[1] #this is the code i judge my string position 1

if mystring == "myjudge":
print mystring = 1
else:
print mystring = 0

I TOTAL WRITE THIS CODE 32 TIME

now I am thinking how I can use for loop to write the code or another simple way to write the code
Reply
#4
Are you trying to know the position of all the 1s in your string or how many ones there are in your string?
Reply
#5
(Oct-02-2019, 09:24 AM)CoderMan Wrote: Are you trying to know the position of all the 1s in your string or how many ones there are in your string?

Good Day,

I would like to know all mystring position event it is 1 or 0
Reply
#6
I don't quite understand but I'm getting that you want to shorten the 32 lines of code. It could be shortened to this -
for x in range(1, 33):
    TEST2 = mystring[:TEST1+x]
Reply
#7
(Oct-02-2019, 12:39 PM)SheeppOSU Wrote: I don't quite understand but I'm getting that you want to shorten the 32 lines of code. It could be shortened to this -
for x in range(1, 33): TEST2 = mystring[:TEST1+x]

Good Day,
If I want judge every mystring(1111111111000000000000000000000) position is under judgement 1 or not how can i add on the judgement.

Thanks for your reply

(Oct-02-2019, 09:24 AM)CoderMan Wrote: Are you trying to know the position of all the 1s in your string or how many ones there are in your string?
My string have 32 position
Reply
#8
Are you trying to get the positions where strTargetData equals mystring at that position?

result = ''
for target_char, my_char in zip(strTargetData, mystring):
    if target_char == my_char:
        result += '1'
    else:
        result += '0'
?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Oct-02-2019, 03:16 PM)ichabod801 Wrote: Are you trying to get the positions where strTargetData equals mystring at that position?
 result = '' for target_char, my_char in zip(strTargetData, mystring): if target_char == my_char: result += '1' else: result += '0' 
?
I am trying use a simple way to judge my code as 1 actually is strTargetData this word is I write wrong.
My string is 1111111111100000000000 I want my code judgement as 1 in my string 1111111111100000000000 in single position

For example
my string [1]#position1 is 1
My judgement =“1”

my string == My judgement :
Print (my string “1”)
Else:
Print (my string “0”)

On this code output I will get 1 cause I judged 1 == 1 is correct

Sorry I have not clear on your Code the result mean is .....why need use zip
Reply
#10
Some language barrier so we are guessing. Do you want:
1. A list of the digits, 1 or 0, from the source string
2. A list of OK or NG depending on whether the source string has 1 or 0
3. Two lists, with the positions of the 1s and 0s.

Which, or if you can rephrase the question and do not use the word judge - that is causing confusion
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can i judge 1st string position is correct number christing 3 2,426 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