I'm pretty new to Python, just going through a good course on Udemy but I have an issue with a little pet project of mine.
I'm trying to write code to help rank favourite games/films etc from a list. The code chooses two items at random from the list then asks the user to choose one. This choice is recorded in a dictionary. A next set of 2 are then given to the user until the number of battles matches the total possible.
All works ok, but I can't figure out how to ensure each new random choice of 2 has not been picked before. My idea was to create a tuple of the two items and append it (and its inverse) to a list. Then check if the random choice is in this list, it would try the random choice again.
I just can't get my head around the ordering of things at all.
The pertinent code is:
import math
my_games = ['A', 'B', 'C']
def random_two(game_list):
import random
two_games = []
while len(two_games) < 2:
rndm = random.choice(game_list)
if rndm not in two_games:
two_games.append(rndm)
else:
pass
return two_games
battle_num = 1
initial_scores = [0]* len(my_games)
game_dict = dict(zip(my_games, initial_scores))
game_battles = []
def choose_games(game_list):
global battle_num
global initial_scores
global game_dict
global game_battles
while tuple(random_two(game_list)) in game_battles:
random_two(game_list)
else: ?
#In here I don't know what to do. Initially "two_game" was just part of this func, but I dropped it outside to call it more easily.
tupl_games1 = tuple(two_games)
tupl_games2 = tuple(two_games[::-1])
game_battles.append(tupl_games1)
game_battles.append(tupl_games2)
choice = int(input(f'Please choose game 1 - {two_games[0]}, or game 2 - {two_games[1]}'))
if choice not in (1,2):
print('You must choose 1 or 2')
choice = int(input(f'Please choose game 1 - {two_games[0]}, or game 2 - {two_games[1]}'))
else:
print(f'You chose {two_games[choice-1]}')
game_dict[two_games[choice-1]] += 1
#print(tupl_games1)
#print(tupl_games2)
#print(battle_num)
#print(game_battles)
battle_num = battle_num +1
Aucun commentaire:
Enregistrer un commentaire