Home > Blockchain >  what does '\r' do and how would I use it in a given scenario? (Python)
what does '\r' do and how would I use it in a given scenario? (Python)

Time:02-24

Ive been trying to make a program for a card game, a quick summary: Write a python script to create a random card, representing the card as a tuple, storing the card value and the color.

One of the conditions was that whilst I loop 3 times, I have to make sure that each card does not have the same values and color as the rest so that no 2 cards are the same. (When the second card is created, check if it is the same as the first and if it is, try again.

When the third card is created, check if it is the same as the first OR the second and if it is, try again.)

I would then have to create a function that adds together the value of 3 cards and returns the sum.

Of course, there is more to it than that but my question is a simple one. So far what I've done:

import random

num = random.randint(0,9)
colour = random.choice(["red", "green", "blue", "yellow"])
card = (num, colour)

def get_uno_card(card):
  for _ in range(1):
    print(card)
    if num and colour == card:
      print(card)
      
  return card
  
def convert():
  a = str(card)
  return a

def add():
  b = num * 3
  return b

a = convert()
b = add()
  
print(get_uno_card(card))
print(a)
print(b)

This outputs:

(6, 'blue')
(6, 'blue')
(6, 'blue')
18

I have trouble trying to make sure the cards are not the same, therefore I was looking for ways to do this online and there was a post where they explained that I should use '/r'. I haven't come across this and don't know how I should use this in this scenario, or if I should use it at all.

Could someone explain how I could check if the 3 tuples are the same and if they are, how would I generate another card in its place?

Many thanks.

CodePudding user response:

\r is a carriage return character, and has nothing at all to do with the problem you're describing, which is one of sampling without replacement.

An easy way to make sure you don't have the same card twice is to make a "deck" of all the cards (which in Python is easily represented as a list) rather than generating each card individually.

With a real deck of cards, you can't get the same card twice because you remove the card from the deck when it's dealt; use your list the same way by popping items from it.

>>> import random
>>> cards = [(num, color) for num in range(10) for color in ["red", "green", "blue", "yellow"]]
>>> random.shuffle(cards)
>>> cards.pop()
(3, 'blue')
>>> cards.pop()
(4, 'yellow')
>>> cards.pop()
(5, 'blue')

Putting this in the context of some of the things you've tried to implement already:

import random

deck = [
    (num, color)
    for num in range(10)
    for color in ("red", "green", "blue", "yellow")
]
random.shuffle(deck)

def get_uno_card():
    return deck.pop()

def add_cards(cards):
    return sum(num for num, color in cards)

cards = [get_uno_card() for _ in range(3)]
print(cards)
print(add_cards(cards))
[(9, 'yellow'), (9, 'blue'), (3, 'yellow')]
21
  • Related