0

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?

Edited by Narue: Fixed code tags

4
Contributors
12
Replies
14
Views
5 Years
Discussion Span
Last Post by adrain91
Featured Replies
  • 1

    I slightly pyTonysed the code, I got same as bro pyguy62 from your version. [CODE]#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 = … Read More

0

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

0

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

0

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"

0

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
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?

1

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)))

Edited by pyTony: n/a

0

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

0

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...

0

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)

Edited by woooee: n/a

0

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)
0

I tried pytony's code ,it worked well.


I'll figure it out .

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.