jeudi 20 avril 2017

"Random" numbers in python are repeating?

After working on a very basic while loop game in python, I discovered that the random.randint function is giving me the same numbers every time I go through the loop again. I've posted the code below if anyone could check it out! Thank you so much in advance!

import random
userNumberone = random.randint (1, 6)
userNumbertwo = random.randint (1, 6)
computerNumberone = random.randint (1, 6)
computerNumbertwo = random.randint (1, 6)
totaluserNumber = userNumberone + userNumbertwo
totalcomputerNumber = computerNumberone + computerNumbertwo
userScore = 0
computerScore = 0

print ("Hello!")
playerName = raw_input ("What's your name?")
print "Hi" ,playerName,"Let's play a dice game! You will roll a dice twice, and so will the computer. The added total of your two die will be your score. The player with the highest score will win, and if it results in a tie, no one wins."

playing = True
while playing == True:

    print "You rolled a " ,userNumberone, "and a ," ,userNumbertwo
    print "The computer rolled a " ,computerNumberone, "and a ," ,computerNumbertwo

    print "The score you rolled was a " ,totaluserNumber, " !"
    print "The score the computer rolled was" ,totalcomputerNumber, "!"

    if totaluserNumber == totalcomputerNumber:
        print "You have tied. Nobody wins this round"
        print "Your score is", userScore
        print "The computer's score is", computerScore

    if totaluserNumber > totalcomputerNumber:
        print "Congratulations, you've won this round!"
        userScore = userScore + 1
        print "Your score is", userScore
        print "The computer's score is", computerScore
    if totalcomputerNumber > totaluserNumber:
        print "Oh no! The computer won this round."
        computerScore = computerScore + 1
        print "Your score is", userScore
        print "The computer's score is", computerScore       

    playagain = raw_input("Do you want to play again? (y/n) ")
    if playagain == "y" or playagain == "Y":
        playing = True
    else:
        playing = False

while playing == False:
    if computerScore> userScore:
        print "Looks like the computer got the best of you this time. Goodbye!"
    elif userScore> computerScore:
        print "You've won the game! See you next time."
    break

Aucun commentaire:

Enregistrer un commentaire