Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want the if-statement to break if the condition is met, because currently if it isn't broken early then I get some mishaps in my code.

The problem is, I am not sure where to put the break. When I put it where it is shown here I get "Unexpected indent", but when I put it back a level I get an error with the else-statement saying "Invalid Syntax".

EDIT: THE IF IS INDENTED. It just didn't show up in the sites code blocks. I will try and fix it on the site.

@duck, what do you think I am trying to do? I am in my first weeks of a python course. I came here to help myself, not get my code trolled by you. If you can help me then I would appreciate the help, otherwise I don't need you telling to "learn how to code" when that's exactly what I am trying to do.

So I am not sure what to do. Any help would be appreciated.

def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
            break
            else:
                yConfirMsg= "No casualties"
share|improve this question
1  
The line immediately following your def should have an indent. –  Rohit Sep 26 '13 at 5:01
3  
Your if after def is not intended –  duck Sep 26 '13 at 5:01
2  
Does your actual code have an indent after the first line? Because it should.. –  Blorgbeard Sep 26 '13 at 5:01
 
I think you need to learn how to write code in python first. Its not good to ask this type of question here. –  duck Sep 26 '13 at 5:03
1  
Your code structure is odd. Why are you doing score = score etc? –  Blorgbeard Sep 26 '13 at 5:04
show 1 more comment

2 Answers

Try this:

def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
    #^This line ident is probably the offending line. ;)
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
                break
            else:
                yConfirMsg= "No casualties"
                score = score #You may want to fix this, since the logic doesn't make sense
                yourHits = yourHits #Fix this line as well. This is variable value assignment to the same variable.

If this doesn't work, another thing to consider is that you may be inadvertently mixing tabs and whitespace when you indent the leading spaces for your code. If so, convert all the tabs to spaces.

And, regarding the notes cited. Perhaps you meant to return those values? If so, you need to fix those logic errors.

UPDATE:

If you only have to break once and only once, then you should replace break with return.

If not, then you should capture the location, continue loop execution, and do whatever you have to do with that information.

    #...
    values = {}
    all_values = []

    for cSpot in AICampLoc:
        if hGuess == cSpot:
            yConfirMsg = "Kill confirmed!!"
            yourHits += 1
            score += 100
            AICampLoc.remove(hGuess)

            values['message'] = yConfirMsg
            values['hits'] = yourHits
            values['score'] = score
            values['camploc'] = AICampLoc

            all_values.append(values)

        else:
            yConfirMsg= "No casualties"
            #...
share|improve this answer
 
That if is indented, I'm not sure why it shows up that way in the code blocks on the site. That is fine. Its the break when I get the errors. And thanks for clearing up my logic errors. EDIT: Also, no whitespacing, everything is tabbed as it should be. –  user2817954 Sep 26 '13 at 5:13
 
@user2817954. I see. I've edited my answer accordingly. –  jrd1 Sep 26 '13 at 5:30
add comment

You are missing an indent, as the other answer states, but also, you have a bunch of code that isn't needed. Your code can be simplified to this:

def pTurn(CampLoc, AICampLoc, score, yourHits, cHits):
    if yourHits < 5:
        hGuess = int(raw_input("Enter a co-ordinate to air-strike: "))
        print "Air-striking co-ordinate: %d" % hGuess
        yConfirMsg= "No casualties"
        for cSpot in AICampLoc:
            if hGuess == cSpot:
                yConfirMsg = "Kill confirmed!!"
                yourHits += 1
                score += 100
                AICampLoc.remove(hGuess)
                break        
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.