JACK
JACK

Reputation: 17

Getting a different random each time python

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()

Upvotes: 0

Views: 105

Answers (5)

Amr El Aswar
Amr El Aswar

Reputation: 3515

Why not use :

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

Instead of putting a whole list ?

Upvotes: 0

Jon Kiparsky
Jon Kiparsky

Reputation: 7743

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()

Upvotes: 3

anto
anto

Reputation: 73

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.

Upvotes: 0

lightandlight
lightandlight

Reputation: 1372

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.

Upvotes: 0

Bhavesh Munot
Bhavesh Munot

Reputation: 675

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

Upvotes: 0

Related Questions