Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How i can judge my code
#11
(Oct-02-2019, 04:48 PM)jefsummers Wrote: 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


sorry to make you confuse I just want to write the code can let me get ok on 1 or ng on 0
How i can use a simple way to write the code and not use complex way

(Oct-02-2019, 04:48 PM)jefsummers Wrote: 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
Sorry to make confuse

1.when mystring first position is 1 and I will print 1 or ok else I will print o or ng
2.mystring total have 32 digit I want write a code to make sure single position 1 is reply ok else position 0 is ng
3 base on my string 32 digit I total will get ok 11time cause I have 11 once 1 and 22 ng cause I have 22 once 0
4 i want my out put is get ok*11 ng*22 or 1111111111000000000000000000000
Reply
#12
(Oct-02-2019, 05:50 PM)christing Wrote: 1.when mystring first position is 1 and I will print 1 or ok else I will print o or ng
2.mystring total have 32 digit I want write a code to make sure single position 1 is reply ok else position 0 is ng
3 base on my string 32 digit I total will get ok 11time cause I have 11 once 1 and 22 ng cause I have 22 once 0
4 i want my out put is get ok*11 ng*22 or 1111111111000000000000000000000
Hi!

Sorry, I think it's still difficult to understand what you want.

Could you use a translator from your mother tongue into English?

Also, can you post your question both in your mother tongue and in English, to see if somebody here understands your mother tongue and helps to answer you?
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#13
still guessing, but maybe

foo = '11111111111000000000000000000000'
for char in foo:
    if char == '1':
        print('ok')
    else:
        print('ng')
lines 3-6 can be replaced with single line:
print('ok' if char == '1' else 'ng')
something else
from collections import Counter
foo = '11111111111000000000000000000000'
print(Counter(foo))
This last snippet will output
Output:
Counter({'0': 21, '1': 11})
You get the number of 0s and 1s
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
#14
(Oct-02-2019, 04:48 PM)jefsummers Wrote: 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
(Oct-02-2019, 04:48 PM)jefsummers Wrote: 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

(Oct-02-2019, 04:11 PM)christing Wrote:
(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
Good Day,
Sorry for make you confuse.

1.the purpose i write the code is want confirm mystring position.
2.if system detect mystring which position (11111111111000000000000000000000)is 1 system will reply 1 or OK else system will reply me NG.
3.in current mystring have 32 number which mean i need confirm 32 time on mystring
4.in mystring have eleven 1 mean my system will reply me 11 number 1 or 11 ok and i have 21 number of 0 mean system will reply me 22 time 0 or NG

if my outpput is use ok or ng as a result my output will reply me okokokokokokokokokokokngngngngngngngngngngngngngngngngngngngng

if my outpput is use 1 or 0 as a result my output will reply me 11111111111000000000000000000000

how i confirm my single mystring position

strData = ["1111111111100000000000 "]
MyConfirm/MyJude = "1"
P1 = strData[1]
P2 = strData[2]
P3 = strData[3]
P4 = strData[4]
P5 = strData[5]
until 32
P32 = strData[32]

if P1 == MyJude :
print('OK')
else:
print('NG')

if P2 == MyJude :
print('OK')
else:
print('NG')

if P3 == MyJude :
print('OK')
else:
print('NG')
I TOTAL WRITE 32 TIME
if P32 == MyJude :
print('OK')
else:
print('NG')

in current situation is i would like use the simple way to write the code and i no need use the complex way to write my code 32time.

(Oct-02-2019, 06:46 PM)buran Wrote: foo = '11111111111000000000000000000000'
for char in foo:
    if char == '1':
        print('ok')
    else:
        print('ng')

YES this is the answer i want.I am no ideal why my python application no success use for method.

But i use online application i get the answer
https://www.tutorialspoint.com/execute_p...online.php
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
ng
this is the answer i want
Reply
#15
(Oct-02-2019, 06:33 PM)newbieAuggie2019 Wrote:
(Oct-02-2019, 05:50 PM)christing Wrote: 1.when mystring first position is 1 and I will print 1 or ok else I will print o or ng
2.mystring total have 32 digit I want write a code to make sure single position 1 is reply ok else position 0 is ng
3 base on my string 32 digit I total will get ok 11time cause I have 11 once 1 and 22 ng cause I have 22 once 0
4 i want my out put is get ok*11 ng*22 or 1111111111000000000000000000000
Hi!

Sorry, I think it's still difficult to understand what you want.

Could you use a translator from your mother tongue into English?

Also, can you post your question both in your mother tongue and in English, to see if somebody here understands your mother tongue and helps to answer you?

Thank you for reply my message i already find out the answer form buran.
foo = '11111111111000000000000000000000'
for char in foo:
if char == '1':
print('ok')
else:
print('ng')
Reply
#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
#17
(Oct-03-2019, 09:38 AM)christing Wrote: Thank you for reply my message i already find out the answer form buran.
foo = '11111111111000000000000000000000'
for char in foo:
if char == '1':
print('ok')
else:
print('ng')

You're welcome! Wink

I'm glad that somebody could help you!

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


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