0

I was trying to make baseball game that counts strike and ball. For example, if random_number is '835' and user_input_number is '853', strike_count should be 1, ball_count should be 2. And this function is only for counting strike and ball. But the program does not run the second for loop for counting ball. So I can't count ball.

def get_strikes_or_ball(user_input_number, random_number):

    random_number_list = list(random_number)
    print("random_number_list: ", random_number_list)
    user_input_number_list = list(user_input_number)
    print("user_input_number_list: ", user_input_number_list)

    strike_count = 0

    for i in range(0, 3):
        print("first for")
        if random_number_list[i] == user_input_number_list[i]:
            strike_count += 1

    print("start")

    ball_count = 0

    for i in range(2, -1):
        print("second for")
        for k in range(1, 3):
            if random_number_list[i] == user_input_number_list[i-k]:
                ball_count += 1

    print("st, ball: ", strike_count, ball_count)
    result = [strike_count, ball_count]

    # ==================================
    return result
1
  • 1
    Try printing [x for x in range(2, -1)] Commented Feb 27, 2018 at 18:17

3 Answers 3

1

The range range(2, -1) is empty. If you want to get the reversed range [-1, 2) you can use reversed(range(-1, 2)).

Sign up to request clarification or add additional context in comments.

1 Comment

You can also use for i in range(2, -1, -1)
0

Python's range function accepts 3 parameters, two of which are optional. Start, stop, and step. Start and step are optional. The default for start is 0, and the default for step is 1. If you want to step in the negative direction, you need to pass -1 for step. Replacing range(2, -1) with range(2, -1, -1) should solve your problem, and this will be more efficient than using the reversed() function.

Comments

0

In for loop parameter, there are three parameters in python for(start, stop(excluded number), step) there must be compulsory to define stop in loop if you don't specify it will get your third parameter as the second parameter and if you have to decrement the value of loop then you must give step as -1 it decrements as value -1 each time.

def get_strikes_or_ball(user_input_number, random_number):

random_number_list = list(random_number)
print("random_number_list: ", random_number_list)
user_input_number_list = list(user_input_number)
print("user_input_number_list: ", user_input_number_list)

strike_count = 0

for i in range(0, 3):
    print("first for")
    if random_number_list[i] == user_input_number_list[i]:
        strike_count += 1

print("start")

ball_count = 0

for i in range(2, -1,-1):
    print("second for")
    for k in range(1, 3):
        if random_number_list[i] == user_input_number_list[i-k]:
            ball_count += 1

print("st, ball: ", strike_count, ball_count)
result = [strike_count, ball_count]

# ==================================
return result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.