Answer the question
In order to leave comments, you need to log in
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))
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question