質問

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()
役に立ちましたか?

解決

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

他のヒント

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.

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

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

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

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,'.')
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top