This is the program I need to write:
At each step the user is asked if she wants another number. If she does, the program adds a random number between 1 and 10 onto her total. The game ends when she quits, or when her total is 18 or higher. If her total is between 18 and 21, she wins and you should print a message saying so. If she goes over 21 or quits before she gets to 18, tell her that she loses. Don't let the program end silently; in every case you should say if the user wins or loses. This is what it should look like

Your current total is 0.
Do you want another number? yes or no: yes
Your number is 2, making your total 2.
Do you want another number? yes or no: yes
Your number is 9, making your total 11.
Do you want another number? yes or no: yes
Your number is 10, making your total 21.
YOU WIN!!!

I pretty much only know "While, If, and For loops as well as break and continue statements.
I can only get to the first randint. After that I'm kind of lost.

``````#Simple blackjack game
#Aces are always equal to one
from random import *
def main():
print "Hello welcome to Blackjack"
print "Your final total must be between 18 and 21 to win"
total=[randint (1,10)]
print "Your first card is %d would you like another card (y/n)"%randint (1, 10),
card= raw_input()
if card=='y' or "Y":
print"You drew a %d. Would you like another card? (y/n)" %randint (1, 10),
print "Your total is " + str(sum(total))
main ()``````

Of course, this ends after the first two draws. The total is a randint (1,10). I guess I don't actually understand what str(sum(total)) does.

## Recommended Answers

Of course, this ends after the first two draws. The total is a randint (1,10). I guess I don't actually understand what str(sum(total)) does.

Alright, you're putting in some effort so I'll help you step-by-step as long as you continue the effort.

First I'll start with that last part …

## All 3 Replies

Of course, this ends after the first two draws. The total is a randint (1,10). I guess I don't actually understand what str(sum(total)) does.

Alright, you're putting in some effort so I'll help you step-by-step as long as you continue the effort.

First I'll start with that last part str(sum(total))

hopefully you know str=string
sum is just that, the sum of what is in the following parenthesis "in this case it's a list with numbers which you have labeled total"

now let's brainstorm what you could do to make it go until the user wins or busts... in other words we want the program to run While the player's score is less than 18 and if the score is > 18 then we need to check if the score is over 21 in which case the player would lose, else the player wins.

I was almost positive I needed to put in a while loop with <18 and else between 18 and 21 but I'm confused about where I have to put it.

Well to be honest the easiest way for me is to use a class, I love them:

``````import random

class Game:
def __init__(self,name):
self.name=name
print ("Hello,",self.name,"welcome to Blackjack")
print("Your final total must be between 18 and 21 to win")
self.score=0
self.draw()

def draw(self):
self.card=random.randint(1,10)
print('Drew a', self.card)
self.score+=self.card
if self.score<18:
self.choose()
elif self.score < 21:
endwin(self.score)
else:
bust()

def choose(self):
print('Your current score is', self.score)
self.choice=raw_input('Select H to hit or Q to quit: ')
self.choice=self.choice.capitalize()
if 'H' in self.choice:
self.draw()
else:
quitlose()

def endwin(score):
print('Score:',score,' you win!')

def bust():
print('You busted, you lose')

def quitlose():
print('You quit before reaching 18, you lose!')

name=raw_input('Enter your name: ')
hand=Game(name)``````

it's a bit ugly and rudimentry, but you can begin to see what you want from it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.