-1

My assignment is in Python 3.3.2 and it is here:

Create a class for a dice object that can randomly generate a number between 1 and 6 and save the file.

You will need the random module and

Create 2 Dice objects a and b and add their values together.

Here are the rules

Win = total equals 7 or 11

Lose = total equals 2,3 or 12

Roll again = total equals 5,6,8,9,10 until a 7 is rolled or the same number is thrown again.


Now the code I have written:

import random

class Dice:
    '''A class that makes Dice'''

    number = random.randint(1,6)

a = Dice
b = Dice

result = a.number + b.number

def resultgiver():
    if result == '7':
        result == '11'
        print('You won! You got ' ,result,'.')

    elif result == '2':
        result == '3'
        result == '12'
        print('You lost! You got ' ,result,'.')

    elif result == '5':
        result == '6'
        result == '8'
        result == '9'
        result == '10'
        print('Roll again! You got ' ,result,'.')

    elif result == '5':
        result == '6'
        result == '8'
        result == '9'
        result == '10'

    elif result == '7':
        result == '11'

resultgiver()
  • 2
    Try instantiating Dice for `a` and `b`, e.g. a = Dice(). Note also that Dice.number will always be the same - the random is evaluated only at import. You probably want to put this in the \_\_init\_\_. – dbn Oct 31 '13 at 20:33

5 Answers5

1

Couple of problems:

  1. you need parentheses around your class instances:

a = Dice() and b = Dice()

  1. result is an integer yet all your if statments check if it equals a char. remove all the quotes around your the numbers

    if result == 5:

  2. you need an init in your class so you always get a different number when you instantiate the class.

    class Dice: '''A class that makes Dice'''

        def __init__(self):
            self.number = random.randint(1,6)
    

try putting a else at the end to catch any result that is not 7 or 5:

elif result == '7':
        result == '11'
else:
    print "we got here"

I think you are trying to emulate a switch statement with your if statement. The way you did wont work but try this:

def resultgiver():
    if result in [7,11]:
        print('You won! You got ' ,result,'.')

    elif result in [2, 3, 12]:
        print('You lost! You got ' ,result,'.')

    elif result in [5, 6, 8, 9, 10]:
        print('Roll again! You got ' ,result,'.')

    else:
        print "default case for result =", result
jramirez
  • 8,537
  • 7
  • 33
  • 46
  • I tried this but still nothing. Thank you for helping though! Oh no wait you changed it and part 2 of your answer helps! Thank you so much! – user2800756 Oct 31 '13 at 20:38
  • Inside each `if` block, it's not clear why there are a series `result == num` lines. There is something going wrong there as well. – SethMMorton Oct 31 '13 at 20:57
  • @SethMMorton you are right, I think OP was trying to emulate the function of a switch statement. Anyways I updated my answer to show a potential approach to achieve that functionality. – jramirez Oct 31 '13 at 21:07
1

What is going wrong? Nothing is printing in Python

You only print anything if the result is 7, 2, or 5, AND for some reason if it is a string (and it's never a string, because you don't convert it to a string). You only set result once, in global scope, so re-running the function doesn't change anything.

Learn about function parameters. You want to pass the number result to your function as a parameter.

Marcin
  • 48,559
  • 18
  • 128
  • 201
1

and you should write

if result == 2 or result == 3 or result == 4:

etc to check two or more conditions. also a.number always equals always b.number, because you assign a value to Dice.number only once. try this:

class Dice(random.Random):
    def number(self):
        return self.randint(1, 6)
a = Dice()
b = Dice()
result = a.number() + b.number()
Sirac
  • 693
  • 4
  • 18
  • When should `result == 2 and result == 3 and result == 4` ever be true? – glglgl Oct 31 '13 at 20:40
  • very sry, was intending to write result == 2 **or** result == 3 etc., i will edit my answer – Sirac Oct 31 '13 at 20:42
  • things like this happen when you Focus on Syntax and Forget the logic. but you have to admit, my Statement is valid – Sirac Oct 31 '13 at 20:43
1

Since this is homework, I'd recommend reading about:

You are doing something wrong in each of these areas in your code.

Community
  • 1
  • 1
dbn
  • 13,144
  • 3
  • 60
  • 86
0

You compare string with integer !!

if result == '7':

I think in this code

if result == '7':
    result == '11'
    print('You won! You got ' ,result,'.')

you want do this

if result == 7 or result == 11 :
    print('You won! You got ' ,result,'.')
furas
  • 134,197
  • 12
  • 106
  • 148