here is my code:

conbi = ['a1','b1','c3','c4','f7']
left_unmatched = 5
wrongtimes = 0
right = 0
guess = input('Shoot: ').split(' ')
guess = list (guess)
for i in range(len(guess)):
    if guess[i] in conbi:
            left_unmatched -=1
            right +=1
            print ('Hit:',right,'Missed: ',left_unmatched)
print ('Hit:',right,'Missed: ',left_unmatched)

when I enter a1 b1 c3 c4 c7:
it should be printed out like "hit:5 missed: 0"but it appeared to be "hit :4 missed :1"

could any one tell me why and hot to correct it?

Recommended Answers

All 12 Replies

I've worked on it for another hour ,but I still did not find anything wrong...

Worked fine for me, line 6 "guess= list(guess)" was unnecessary, when you split it, you're splitting the string into a list. Also, how are you going to account for user error? ie. what if I entered: Shoot: f7, c4, c3, b1, a1

Thanks for your reply.
1 that's just part of my whole program,I didn't post it all.
2 I TRIED THE CODE AGAIN , IT STILL DOES NOT WORK OUT:IT'S STILL"hit :4 missed :1"

Thanks for your reply.
1 that's just part of my whole program,I didn't post it all.
2 I TRIED THE CODE AGAIN , IT STILL DOES NOT WORK OUT:IT'S STILL"hit :4 missed :1"

hmm that's weird, I got:

Shoot: a1 b1 c3 c4 f7
Hit: 1 Missed:  4
Hit: 2 Missed:  3
Hit: 3 Missed:  2
Hit: 4 Missed:  1
Hit: 5 Missed:  0
Hit: 5 Missed:  0

in wing IDE python 3 shell:

Shoot: a1 b1 c3 c4 c7
Hit: 1 Missed: 4
Hit: 2 Missed: 3
Hit: 3 Missed: 2
Hit: 4 Missed: 1
Hit: 4 Missed: 1

GOT ANY IDEA ,BRO?

Try it in IDLE, see what happens

I slightly pyTonysed the code, I got same as bro pyguy62 from your version.

#Python 2 compatibility
from __future__ import print_function
try:
    input = raw_input
except:
    pass

# valid 10 x 10 square cordinates
valid = {a+str(b) for a in 'abcdefghij' for b in range(10)}

combination = {'a1','b1','c3','c4','f7'}
hits = []
checked = []

for guess in input('Shoot: ').lower().split(' '):
    if guess not in valid:
        print('Invalid: {}(discarded)'.format(repr(guess)))
    else:
        if guess in combination:
            print ('Hit: {}'.format(repr(guess)))
            hits.append(guess)
        checked.append(guess)
        
print ('Hit:',len(hits),'Missed: ', len(checked) - len(hits))

## with sets
shots = set(input('With sets, Shoot: ').lower().split(' '))

invalid = shots - valid
if invalid:
    print('Invalid shots: {}'.format(', '.join(invalid)))
    shots -= invalid

hits = shots & combination
missed = shots - hits

print('Hits: {hits}, Missed: {missed}'.format(hits=len(hits), missed=len(missed)))

tried in shell , tried in idle, my python 3 still sucked on
"
Hit: 1 Missed: 4
Hit: 2 Missed: 3
Hit: 3 Missed: 2
Hit: 4 Missed: 1
Hit: 4 Missed: 1
"

If I can upload a photo ,I will show you the screenshot

That's not really necessary, did you try running our codes as well? Maybe we should see more of the code, I don't see how it could be relevant but obviously something ridiculous is going wrong...

Everyone should also print what was entered.

conbi = ['a1','b1','c3','c4','f7']
left_unmatched = 5
wrongtimes = 0
right = 0
guess = input('Shoot: ').split(' ')  ## guess is already a list because of the split()
print "guess entered =", guess

guess = list (guess)
for ctr in range(len(guess)):
    print "     checking for", guess[ctr]
    if guess[ctr] in conbi:
            left_unmatched -=1
            right +=1
            print ('Hit:',right,'Missed: ',left_unmatched)
print ('Hit:',right,'Missed: ',left_unmatched)

OP is using Python3, so woooee's printfull code should read:

#Python 2 compatibility
from __future__ import print_function
try:
    input = raw_input
except:
    pass

conbi = ['a1','b1','c3','c4','f7']
left_unmatched = 5
wrongtimes = 0
right = 0
guess = input('Shoot: ').split(' ')  ## guess is already a list because of the split()
print("guess entered =", guess)

guess = list (guess)
for ctr in range(len(guess)):
    print("     checking for", guess[ctr])
    if guess[ctr] in conbi:
            left_unmatched -=1
            right +=1
            print ('Hit:',right,'left: ',left_unmatched)
print ('Hit:',right,'Missed: ',left_unmatched)

I tried pytony's code ,it worked well.


I'll figure it out .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.