populate each hand with two cards. Take a card from the deck
and put it in the player_hand list. Then, take a card from the deck and
put it in the dealer_hand list. Do that one more time in that order so that the dealer and player have each two cards. Make sure the dealer's first card is face down. I keep receiving this error from my 2 tests.
My code:
while len(dealer_hand) != 2 and len(player_hand) != 2:
player_card = random.choice(deck)
player_hand.append(player_card)
deck.remove(player_card)
if len(player_hand) == 2:
player_hand[0].face_up()
player_hand[1].face_up()
dealer_card = random.choice(deck)
dealer_hand.append(dealer_card)
deck.remove(dealer_card)
if len(dealer_hand) == 2:
dealer_hand[0].face_down()
dealer_hand[1].face_up()
return player_hand and dealer_hand
False != True
Expected :True
Actual :False
def test_deal_cards():
deck = []
for suit in cards.SUITS:
for rank in cards.RANKS:
deck.append(cards.Card(suit, rank))
dealer_hand = []
player_hand = []
blackjack.deal_cards(deck, dealer_hand, player_hand)
assert len(dealer_hand) == 2
assert len(player_hand) == 2
> assert dealer_hand[0].is_face_up() is True
E assert False is True
E + where False = <bound method Card.is_face_up of [10 of Hearts]>()
E + where <bound method Card.is_face_up of [10 of Hearts]> = [10 of Hearts].is_face_up
test_deal_cards.py:19: AssertionError
(test_deal_cards_alternates_between_player_and_dealer)
[7 of Hearts] != [6 of Spades]
Expected :[6 of Spades]
Actual :[7 of Hearts]
def test_deal_cards_alternates_between_player_and_dealer():
card1 = cards.Card(cards.SPADES, cards.SIX)
card2 = cards.Card(cards.HEARTS, cards.SEVEN)
card3 = cards.Card(cards.CLUBS, cards.EIGHT)
card4 = cards.Card(cards.DIAMONDS, cards.NINE)
deck = [card4, card3, card2, card1]
dealer_hand = []
player_hand = []
blackjack.deal_cards(deck, dealer_hand, player_hand)
assert len(dealer_hand) == 2
assert len(player_hand) == 2
> assert player_hand[0] is card1, 'Player 1st card should be Six of Spades'
E AssertionError: Player 1st card should be Six of Spades
E assert [7 of Hearts] is [6 of Spades]
test_deal_cards.py:39: AssertionError
I think the error lies within the first line:
while len(dealer_hand) and len(player_hand) != 2:
If you want to check if the dealer also has 2 cards, you need to fix that line to:
while len(dealer_hand) != 2 and len(player_hand) != 2:
Related
I do not know how to draw 2 or more different numbers and put them in another list, so far I have tried this:
import random
def jugar(m):
m = list(range(1, 14, 1)) * m
random.shuffle(m)
player1 = []
for n in m:
random.choice(m)
player1.append(n)
if n + n == 21:
print("Nano jack")
elif n + n < 21:
random.choice(m)
player1.append(n)
elif n + n > 21:
print("Loser")
return player1
jugar(1)
but this returns me 2 equal numbers, it is similar to the game of blackjack, I want it to keep adding random numbers until it reaches 21 or more, thanks for the help in advance
You can use choice method to randomly select an item from a given list.
Use it in a for loop to randomly select more items.
import random
the_list = [1, 2, 3, 4]
new_list = []
for i in range(3):
value = random.choice(the_list)
new_list.append(value)
This is my code. I have looked at many similar codes and have mine set up exactly how I should. I do not receive an error!
The problem is the output I receive is [11]. When the user inputs [1,2,3,4,5,6,7,8,9,11]. Why is it only pulling one odd number??
totlist = []
max_int = 10
oddlist = []
while len(totlist) < max_int:
nums = int(input('Enter a number: '))
totlist.append(nums)
def find_odds(totlist, oddlist):
if len(totlist) == 0:
return
v = totlist.pop()
if v % 2 == 1:
oddlist.append(v)
find_odds(totlist,oddlist)
print(oddlist)
You have forgoot the loop boucle inside the function
def find_odds(totlist, oddlist):
for item in range(len(totlist)) : # here
if len(totlist) == 0:
return
v = totlist.pop()
if v % 2 == 1:
oddlist.append(v)
I have tried to find an answer to this in vain, so here goes:
The goal is to have a dictionary that has a few lists as values, and then have a function that (depending on user input) will take one of those lists and combine it with other lists, and finally I should get the final list printed.
Seems simple enough but what I get is a type error (lists being unhashable). The combine2 function seems to be working perfectly fine with any other two lists I try to feed it, except for when it tries to get a list that is a dictionary value (??). Does anybody know what I'm doing wrong?
dic = {
'reptiles': ['lizzard', 'crocodile', 'T-Rex'],
'birds': ['canary', 'parrot', 'seagul'],
'mammals': ['monkey', 'cat', 'dog'],
'insects': ['ant', 'bee', 'wasp']
}
FishList = ['goldfish', 'shark', 'trout']
def combine2 (a, b): # returns the combinations of 2 lists' items
tmp = []
n = 0
while n < len(a):
for i in b:
if 8 <= len(str(a[n])+str(i)) and 16 >= len(str(a[n])+str(i)):
tmp.append(str(a[n]) + str(i))
n += 1
return tmp
def animals_mix(k, l): # just some arbitrary combinations of lists
list1 = combine2(FishList, dic[k])
list2 = combine2(list1, dic[k])
list3 = combine2(dic[k], FishList)
l = dic[k] + list1 + list2 + list3
def animals():
print '''\n\nwhat's your favourite animal group?\n
1) reptiles
2) birds
3) mammals
4) insects
'''
while True:
x = raw_input("[+] pick a number > ")
tmp = []
if x == '1':
animals_mix(dic['reptiles'], tmp)
break
elif x == '2':
animals_mix(dic['birds'], tmp)
break
elif x == '3':
animals_mix(dic['mammals'], tmp)
break
elif x == '4':
animals_mix(dic['insects'], tmp)
break
elif x == '':
break
else:
print "\nError: That wasn't in the list of options\nType one of the numbers or press ENTER to move on\n"
return tmp
print animals()
For "TypeError: unhashable type: 'list'", it is because you are actually passing the list in your dict when you seemingly intend to pass the key then access that list:
animals_mix(dic['reptiles'], tmp)
...
def animals_mix(k, l):
list1 = combine2(FishList, dic[k])
in the first line of animals_mix() you are actually trying to do dic[dic['reptiles']] and dicts can not be keyed by un-hashable types, hence the error.
I have a list like below
list1 = [cont1,cont2,cont4,cont5]
how do i implement round robin logic in python to select elements of a list,
each time i try to access element
I'd suggest using itertools.cycle.
Make an iterator returning elements from the iterable and saving a
copy of each. When the iterable is exhausted, return elements from the
saved copy. Repeats indefinitely.
Sample usage:
seq = [1,2,3,4]
round_robin = itertools.cycle(seq)
assert round_robin.next() == 1
assert round_robin.next() == 2
assert round_robin.next() == 3
assert round_robin.next() == 4
assert round_robin.next() == 1
assert round_robin.next() == 2
assert round_robin.next() == 3
assert round_robin.next() == 4
seq = [1,2,3,4]
n = -1
def round_rob_seq():
global n
n = n + 1
return seq[n % len(seq)]
or
def round_rob_seq():
global n
n = n + 1
if n == len(seq):
n = 0
return seq[n]
I need to remove the unique elements of the list, the first thought is:
def cut_uniq(data):
for x in data:
if data.count(x) == 1:
data.remove(x)
print(data)
cut_uniq([1, 2, 3, 4, 5,])
return
[2, 4]
please, tell me why?
Look at each iteration:
i x data
0 1 [1,2,3,4,5]
1 3 [2,3,4,5]
2 5 [2,4,5]
[2,4]
You can iterate over a different list than you are modifying. This returns a copy of the list
def cut_uniq(data):
return [x for x in data if data.count(x) > 1]
or more efficiently
from collection import Counter
def cut_uniq(data):
return [x for x, count in Counter(data) if count > 1]
If you really do want to modify the original list, and not return a copy
def cut_uniq(data):
i = 0
while i < len(data):
if data.count(data[i]) == 1:
del data[i]
else:
i += 1
or
from collections import Counter
def cut_uniq(data):
for x, count in Counter(data):
if count == 1:
data.remove(x)
95% of the time that you modify the same list as you're iterating over, you'll have problems.
When you use
for x in data:
it translates to
for i in [0,1,2,3,4]:
x = data[i]
So in the first loop, i = 0 data[i]=1. you remove 1 from data, the data is [2,3,4,5]
on the second loop , i = 1, because now data is [2,3,4,5], data[i] = 3. So 2 is left in the data list and never been visited.
Same as the number 4.
So when you finished your loop, the [2,4] leted in the list.