Can someone fix my code? (again)

I posted a code earlier asking about my Python3 code. I got a satisfactory answer by @philshem, but now my code is still not working. Here is the code:

#Dice Game
from random import randint
score = 0
running = True
while running:
    print ('Rolling dice... rolling dice...')
    print ('Done!')
    rollcode = randint(1, 6)
    if rollcode == randint:
        print ('You scored: 1')
        score = score + 1
        running = False
    else:
        running = False
    if rollcode == randint:
        print ('You scored: 2')
        score = score + 2
        running = False
    else:
        running = False
    if rollcode == randint:
        print ('You scored: 3')
        score = score + 3
        running = False
    else:
        running = False
    if rollcode == randint:
        print ('You scored: 4')
        score = score + 4
        running = False
    else:
        running = False
    if rollcode == randint:
        print ('You scored: 5')
        score = score + 5
        running = False
    running = False
    if rollcode == randint:
        print ('You scored: 6')
        score = score + 6
        running = False
    else:
        running = False

Now when I run it, it doesn't print what I want. It says

Rolling dice... rolling dice... Done! But it never prints what you scored.

Upvotes: 0

Views: 49

Answers (2)

Tony
Tony

Reputation: 744

This will probably do what you're intending with less code and print your total score.

from random import randint

score = 0
running = True

while running:
    #Generate Random number 1-6
    print ('Rolling dice... rolling dice...')
    rollValue = randint(1,6)

    #Print roll value
    print ('Done! \nYou scored: ' + str(rollValue))

    #Add roll value to total score and print score
    score += rollValue
    print('Your total score is: ' + str(score))

    #roll again (y or Y) or end program?
    print ('Roll Again? [Y/N]')
    answer = input().lower()
    if answer != 'y':
        running = False

Upvotes: 0

erewok
erewok

Reputation: 7845

Near the top, you have the following line:

rollcode = randint(1, 6)

Then, all of your branches are checking the exact same thing:

if rollcode == randint:

randint is a function, and because you assigned a random integer value to rollcode, rollcode will never be exactly what randint is.

It looks like what you wanted to do was something like:

if rollcode == 1:
   ...
if rollcode == 2:
   etc.

Upvotes: 1

Related Questions