Python list doesn't seem to be updating
I'm fairly new to python and I'm trying to create blackjack. However, when I'm trying to print out what the player's hand is, I run into a few difficulties.
This is my code for hitting (drawing a card):
def hit(card, deck):
global money, choice
choice = input("How much would you like to bet?n")
money -= int(choice)
print("You have decided to bet $" + str(choice))
card = card.drawCard(deck.deck)
card.getPt()
deck.addScore(card)
deck.addCard(card)
c = str(card)
p = str(deck)
print("You have drawn: " + str(c) + "n")
print("The player has:n" + str(p) + "n")
print("Total score:", deck.score)
And this is my code for printing my cards out:
def __str__(self):
for i in range(0, len(self.deck)):
self.print = self.print + "n" + str(self.deck[i])
return self.print
The first thing my code does is draw two cards for the dealer and the player, which runs fine. However, after the player draws a card is where it gets a bit wonky. The output is something like this:
The player has drawn Card A
The player has drawn Card B
Total score: number
How much would you like to bet?
number
You have bet number
You have drawn Card B
Player has:
Card A
Card B
Card A
Card B
Card B
When I draw a new card, the card doesn't change, it stays the last card I drew. Then, when I print my deck, it prints my old deck and my new deck. However, the score is correct, which indicates that my list is only three cards long. What is going on, and why is it printing five cards?
Full code
Example output
python list class blackjack
add a comment |
I'm fairly new to python and I'm trying to create blackjack. However, when I'm trying to print out what the player's hand is, I run into a few difficulties.
This is my code for hitting (drawing a card):
def hit(card, deck):
global money, choice
choice = input("How much would you like to bet?n")
money -= int(choice)
print("You have decided to bet $" + str(choice))
card = card.drawCard(deck.deck)
card.getPt()
deck.addScore(card)
deck.addCard(card)
c = str(card)
p = str(deck)
print("You have drawn: " + str(c) + "n")
print("The player has:n" + str(p) + "n")
print("Total score:", deck.score)
And this is my code for printing my cards out:
def __str__(self):
for i in range(0, len(self.deck)):
self.print = self.print + "n" + str(self.deck[i])
return self.print
The first thing my code does is draw two cards for the dealer and the player, which runs fine. However, after the player draws a card is where it gets a bit wonky. The output is something like this:
The player has drawn Card A
The player has drawn Card B
Total score: number
How much would you like to bet?
number
You have bet number
You have drawn Card B
Player has:
Card A
Card B
Card A
Card B
Card B
When I draw a new card, the card doesn't change, it stays the last card I drew. Then, when I print my deck, it prints my old deck and my new deck. However, the score is correct, which indicates that my list is only three cards long. What is going on, and why is it printing five cards?
Full code
Example output
python list class blackjack
Why do you generate a random number for drawing a card, instead of shuffling the deck once and just popping from the top?
– user3483203
Nov 15 '18 at 3:43
add a comment |
I'm fairly new to python and I'm trying to create blackjack. However, when I'm trying to print out what the player's hand is, I run into a few difficulties.
This is my code for hitting (drawing a card):
def hit(card, deck):
global money, choice
choice = input("How much would you like to bet?n")
money -= int(choice)
print("You have decided to bet $" + str(choice))
card = card.drawCard(deck.deck)
card.getPt()
deck.addScore(card)
deck.addCard(card)
c = str(card)
p = str(deck)
print("You have drawn: " + str(c) + "n")
print("The player has:n" + str(p) + "n")
print("Total score:", deck.score)
And this is my code for printing my cards out:
def __str__(self):
for i in range(0, len(self.deck)):
self.print = self.print + "n" + str(self.deck[i])
return self.print
The first thing my code does is draw two cards for the dealer and the player, which runs fine. However, after the player draws a card is where it gets a bit wonky. The output is something like this:
The player has drawn Card A
The player has drawn Card B
Total score: number
How much would you like to bet?
number
You have bet number
You have drawn Card B
Player has:
Card A
Card B
Card A
Card B
Card B
When I draw a new card, the card doesn't change, it stays the last card I drew. Then, when I print my deck, it prints my old deck and my new deck. However, the score is correct, which indicates that my list is only three cards long. What is going on, and why is it printing five cards?
Full code
Example output
python list class blackjack
I'm fairly new to python and I'm trying to create blackjack. However, when I'm trying to print out what the player's hand is, I run into a few difficulties.
This is my code for hitting (drawing a card):
def hit(card, deck):
global money, choice
choice = input("How much would you like to bet?n")
money -= int(choice)
print("You have decided to bet $" + str(choice))
card = card.drawCard(deck.deck)
card.getPt()
deck.addScore(card)
deck.addCard(card)
c = str(card)
p = str(deck)
print("You have drawn: " + str(c) + "n")
print("The player has:n" + str(p) + "n")
print("Total score:", deck.score)
And this is my code for printing my cards out:
def __str__(self):
for i in range(0, len(self.deck)):
self.print = self.print + "n" + str(self.deck[i])
return self.print
The first thing my code does is draw two cards for the dealer and the player, which runs fine. However, after the player draws a card is where it gets a bit wonky. The output is something like this:
The player has drawn Card A
The player has drawn Card B
Total score: number
How much would you like to bet?
number
You have bet number
You have drawn Card B
Player has:
Card A
Card B
Card A
Card B
Card B
When I draw a new card, the card doesn't change, it stays the last card I drew. Then, when I print my deck, it prints my old deck and my new deck. However, the score is correct, which indicates that my list is only three cards long. What is going on, and why is it printing five cards?
Full code
Example output
python list class blackjack
python list class blackjack
asked Nov 15 '18 at 3:41
Alex RuanAlex Ruan
84
84
Why do you generate a random number for drawing a card, instead of shuffling the deck once and just popping from the top?
– user3483203
Nov 15 '18 at 3:43
add a comment |
Why do you generate a random number for drawing a card, instead of shuffling the deck once and just popping from the top?
– user3483203
Nov 15 '18 at 3:43
Why do you generate a random number for drawing a card, instead of shuffling the deck once and just popping from the top?
– user3483203
Nov 15 '18 at 3:43
Why do you generate a random number for drawing a card, instead of shuffling the deck once and just popping from the top?
– user3483203
Nov 15 '18 at 3:43
add a comment |
1 Answer
1
active
oldest
votes
Well, to get to the answer in short: you never reset Deck.print. So it keeps accumulating at each call to __str__
But in general this code could be improved significantly
E.g. your __str__ function is far from being pythonic. Something along the lines of
return 'n'.join(self.deck)
would look much better.
In general there is no need to prepend each variable with "self." if they are used within the function only. In most cases in a class method you either update an object variable (self.something) or return some value from the function, but not both.
There may, of course, be exceptions to this, but it seems in your case this is the rule and not the exception.
Using globals is also something you should avoid whenever possible.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312104%2fpython-list-doesnt-seem-to-be-updating%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, to get to the answer in short: you never reset Deck.print. So it keeps accumulating at each call to __str__
But in general this code could be improved significantly
E.g. your __str__ function is far from being pythonic. Something along the lines of
return 'n'.join(self.deck)
would look much better.
In general there is no need to prepend each variable with "self." if they are used within the function only. In most cases in a class method you either update an object variable (self.something) or return some value from the function, but not both.
There may, of course, be exceptions to this, but it seems in your case this is the rule and not the exception.
Using globals is also something you should avoid whenever possible.
add a comment |
Well, to get to the answer in short: you never reset Deck.print. So it keeps accumulating at each call to __str__
But in general this code could be improved significantly
E.g. your __str__ function is far from being pythonic. Something along the lines of
return 'n'.join(self.deck)
would look much better.
In general there is no need to prepend each variable with "self." if they are used within the function only. In most cases in a class method you either update an object variable (self.something) or return some value from the function, but not both.
There may, of course, be exceptions to this, but it seems in your case this is the rule and not the exception.
Using globals is also something you should avoid whenever possible.
add a comment |
Well, to get to the answer in short: you never reset Deck.print. So it keeps accumulating at each call to __str__
But in general this code could be improved significantly
E.g. your __str__ function is far from being pythonic. Something along the lines of
return 'n'.join(self.deck)
would look much better.
In general there is no need to prepend each variable with "self." if they are used within the function only. In most cases in a class method you either update an object variable (self.something) or return some value from the function, but not both.
There may, of course, be exceptions to this, but it seems in your case this is the rule and not the exception.
Using globals is also something you should avoid whenever possible.
Well, to get to the answer in short: you never reset Deck.print. So it keeps accumulating at each call to __str__
But in general this code could be improved significantly
E.g. your __str__ function is far from being pythonic. Something along the lines of
return 'n'.join(self.deck)
would look much better.
In general there is no need to prepend each variable with "self." if they are used within the function only. In most cases in a class method you either update an object variable (self.something) or return some value from the function, but not both.
There may, of course, be exceptions to this, but it seems in your case this is the rule and not the exception.
Using globals is also something you should avoid whenever possible.
answered Nov 15 '18 at 4:32
g.ag.a
9615
9615
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312104%2fpython-list-doesnt-seem-to-be-updating%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why do you generate a random number for drawing a card, instead of shuffling the deck once and just popping from the top?
– user3483203
Nov 15 '18 at 3:43