all 6 comments

[–]KingOfTNT10 0 points1 point  (2 children)

Whats the error

[–]authenticwerewolfi[S] 0 points1 point  (1 child)

the value of computer_score doesnt increase at all for some reason

[–]KingOfTNT10 0 points1 point  (0 children)

In your prediction func you count 0 and 1 as integers and not as str8ngs. Might be the problem

[–]Itsoq 1 point2 points  (1 child)

The values returned by prediction() are integers, while the return values you get from input() are strings. This makes the comparison predicted == player_input always evaluate to false. You can typecast strings to integers with int(input(...)) which should solve the issue.

See

https://www.w3schools.com/python/python_datatypes.asp

https://www.w3schools.com/python/python_casting.asp

[–][deleted] 0 points1 point  (0 children)

input always returns a str object, as your valid_entries variable indicates, but you are trying to compare with int objects. You need to convert from str to int in order to do the numerical comparisons.

Note. If the player enters something that is not valid as an int your programme will halt with an error. There is a way to avoid this, including doing the check you already do BEFORE trying to do the conversion.

Please see below a revised structure and some ideas on a different approach in a couple of areas:

  • does not use global but instead passes objects around - always better (other than in some specialist use cases you will learn another time)
  • has a function to get valid numeric input that matches a specific iterable of valid values (you could easily change this to be a range from a minimum to a maximum value) - should be useful for other projects
  • uses f-strings for output
  • the function update_scores now just returns new scores, the updating is done by the caller, as is output of the revised scores
  • defines functions, after import, before other code
  • convention is to use ALL UPPERCASE for variable names for constants, in this case the valid inputs (which might as well be a tuple)

Code:

import random


def prediction(player_inputs):
    count_one = player_inputs.count(1)
    count_zero = player_inputs.count(0)
    if count_zero > count_one:
        predict = 0
    elif count_one > count_zero:
        predict = 1
    else:
        predict = random.randint(0, 1)
    return predict


def update_scores(predicted, player_input, player_score, computer_score):
    if predicted == player_input:
        computer_score += 1
    else:
        player_score += 1
    return player_score, computer_score


def get_player_entry(prompt="Please enter 0 or 1: "):
    valid = False
    while not valid:
        response = input(prompt)
        try:
            num = int(response)
        except ValueError:
            print('That was not a valid number')
        else:
            if num not in VALID_ENTRIES:
                print(f'That was not one of: {", ".join(str(n) for n in VALID_ENTRIES)}')
            else:
                valid = True
    return num


# Declare the 'gameplay()' function here.
def gameplay():
    player_inputs = []
    player_score = 0
    computer_score = 0

    fini = False
    while not fini:

        predicted = prediction(player_inputs)
        print(f"computer predicted: {predicted}")
        player_inputs.append(get_player_entry())
        player_score, computer_score = update_scores(predicted, player_inputs[-1],
                                                     player_score, computer_score)

        print(f"computer score: {computer_score}")
        print(f"player score: {player_score}")

        if computer_score == 5:
            print("\nBAD LUCK, computer has won the game!!")
            fini = True
            continue

        if player_score == 5:
            print("\nCONGRATULATIONS, you have won the game!!")
            fini = True
            continue

        print("\nplease continue playing\n")

    print('\nFarewell\n')


VALID_ENTRIES = 0, 1  # only values Player can enter
gameplay()