I
I
Islam Azikov2020-07-12 20:20:22
Python
Islam Azikov, 2020-07-12 20:20:22

How to add a variable to a function that changes depending on the result?

You need to make a function that compares the input value (1 or 2) with a random number (1 or 2) and, depending on the result, displays a score that shows who won.
For example:
I entered (2) and did not match the random number - score 0 1
the function repeated and already (2) matched the random number - score 1 1, etc. until the score of one of the parties becomes equal to 5.
i wrote this:

from random import randint
def game(i):
    comp = randint(1, 2)
    i_coin = 0
    comp_coin = 0
    while i_coin < 5 and comp_coin < 5:
        if i == comp:
            i_coin = i_coin + 1
            print(i_coin, comp_coin)
        else:
            comp_coin = comp_coin + 1
            print(i_coin, comp_coin)
print(game(1))

But either 0 1, 0 2, 0 3, 0 4, 0 5 or 1 0, 2 0, 3 0, 4 0, 5 0 appears on the screen.
That is, after one comparison, the score simply increases to 5, but at the same time a new repetition is not initiated. Either I have not yet learned how to do it, or I misunderstood something about cycles. In general, please tell me in a language accessible to a beginner, what is my mistake. Thank you very much.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dadasay, 2020-07-12
@Dzharves

def game(i):
    i_coin = 0
    comp_coin = 0
    while True:
        if not (i_coin < 5 and comp_coin < 5):
            break
        new_random_value = randint(1, 2)
        if i == new_random_value:
            i_coin = i_coin + 1
            print(i_coin, comp_coin)
        else:
            comp_coin = comp_coin + 1
            print(i_coin, comp_coin)

print(game(1))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question