0

so the program runs well. The issue is, if I want to play again, it calls the same random numbers each time. How do I make it so I calls a different random number and card each time I want to play again? Thanks.

import random
import math
import time
import sys

Hearts = "Hearts"
Clubs = "Clubs"
Diamonds = "Diamonds"
Spades = "Spades"

number1 = [1,2,3,4,5,6,7,8,9,10]
number1 = random.choice(number1)
number2 = [1,2,3,4,5,6,7,8,9,10]
number2 = random.choice(number2)
number3 = [1,2,3,4,5,6,7,8,9,10]
number3 = random.choice(number3)

card1 = [Hearts, Clubs, Diamonds, Spades]
card1 = random.choice(card1)
card2 = [Hearts, Clubs, Diamonds, Spades]
card2 = random.choice(card2)
card3 = [Hearts, Clubs, Diamonds, Spades]
card3 = random.choice(card3)

blacklist = ["King" "Queen", "Jack"]
blacklist = random.choice(blacklist)

ace = ["Ace"]
ace = random.choice(ace)

runningtotal = number1 + number2
roundtotal = runningtotal + number3

def startup():
    print("Starting game... Your first cards are being drawn")
    round1()

def round1():
    if number1 == 10:
        print ("{} of {}".format(blacklist, card1))
        round2()
    elif number1 == 1:
        print("{} of {}".format(ace, card1))
        round2()
    else:
        print ("{} of {}".format(number1, card1))
        round2()

def round2():
    if number2 == 10:
        print ("{} of {}".format(blacklist, card2))
        round3()
    elif number2 == 1:
        print("{} of {}".format(ace, card2))
        round3()
    else:
        print ("{} of {}".format(number2, card2))
        round3()

def round3():
    startr3 = input("Would you like to be dealt another card? Your total is... {}".format(runningtotal))
    if startr3 == "yes":
        if number3 == 10:
            print("{} of {}".format(blacklist, card3))
        elif number3 == 1:
            print("{} of {}".format(ace, card3))
        else:
            print("{} of {}".format(number3, card3))
            if roundtotal >= 22:
                loose()
            else:
                print("You win!")
                again = input("Do you want to play again?")
                if again == "yes":
                    round1()
                elif again == "no":
                    endgame()

def endgame():
    print("Thankyou for playing")

def loose():
    looseg = input("Your total was above 21! Do you want to play again?")
    if looseg == "yes":
        number1()
    elif looseg == "no":
        endgame()
startup()
0

5 Answers 5

3

Actually, the real problem here is that you only choose your numbers once, when the program starts up. Instead, you need to pick them each time they're needed. like this:

def round1():
    number1 = random.randint(1, 10)
    if number1 == 10:
        print ("{} of {}".format(blacklist, card1))
        round2()
    elif number1 == 1:
        print("{} of {}".format(ace, card1))
        round2()
    else:
        print ("{} of {}".format(number1, card1))
        round2()
Sign up to request clarification or add additional context in comments.

2 Comments

How would I go about getting a running total then? (Im very new to python :P)
You could pass relevant values to the functions that you're calling, and keep the state that way, or you could set up a global variable and update that as you go.
0

Calling random.seed() method once at the start of your code should solve the problem.

1 Comment

Great answer - if you're writing C. python's random is a well-behaved module, it knows to seed itself. random.seed() is mostly useful for setting up a specific starting state for the random generator - that is, for getting the same pseudorandom sequence each time, ie, for testing.
0

You need to initialize the generator using random.seed(), which will use the system time as a seed.

Currently your program is using the same seed each time you run it.

Comments

0

One easy way would be to call a "setup" function before calling round1() in this part of the code (the part where the player loses too):

# ...
if again == "yes":
    # here
    round1()
elif again == "no":
#...

This function would need to change the values of the global variables you define in the beginning. That way, you would have a different hand.

Comments

0

Why not use :

from random import randint
number1 = randint(0,10)

Instead of putting a whole list ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.