Function does not work as expected

Hi all, I am on Julia 1.1.1 within Jupyter kernel. I wrote a dice program, but I get errors. Not sure what I am doing wrong? I consulted the docs with no gains.

function playcr(bet_amount ::Int64)
    die1 = rand(1:6, 1)
    die2 = rand(1:6, 1)
    roll = die1+die2
    if roll == 7 | roll == 11
        print("You won!")
        return bet_amount
    elseif roll == 2 | roll == 3 | roll == 12
        print("Woops, you lost.")
        return -bet_amount
    else
        score = roll
        print()
        print("You rolled a ", score, ".")
    roll = diceroll()
    while roll != 7 & roll != score
        roll = diceroll()
        print("Now you rolled a ", roll)
    if roll == 7
        print("Too bad, you lost.")
        return -bet_amount
    else
        print("You won! Congrats!")
        return bet_amount
                end
            end
        end
    end

First you want to use logical || operator, not the bitwise | operator:

if roll == 7 || roll == 11

and

elseif roll == 2 || roll == 3 || roll == 12

But after this, the function

diceroll()

is not defined for me, but probably for you.

In general, consider the following:

2 Likes

In the above If statements you are testing arrays.
roll is an arrray of length 1
I am not sure what you want, but mayby you want a random scalar, e.g.:
die1 = rand(1:6, 1)[1]

1 Like

Or even easier:
die1 = rand(1:6)

I have opend an issue:
https://github.com/JuliaLang/julia/issues/32871

1 Like

They’re listed under control flow:

https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation-1

2 Likes

Thank you very much!

Your answer works!

Best,

Abdul Naaser

PS: here is the corrected version for anyone else new to Julia:

function playcr(bet_amount ::Int64)
    die1 = rand(1:6)
    die2 = rand(1:6)
    roll = die1+die2
    if roll == 7 || roll == 11
        print("You won!")
        return bet_amount
    elseif roll == 2 || roll == 3 || roll == 12
        print("Woops, you lost.")
        return -bet_amount
    else
        score = roll
        print()
        print("You rolled a ", score, ".")
    while roll != 7 && roll != score
        print("Now you rolled a ", roll)
    if roll == 7
        print("Too bad, you lost.")
        return -bet_amount
    else
        print("You won! Congrats!")
        return bet_amount
                end
            end
        end
    end


I appreciate your reply. I should have seen this earlier but I was being overconfident (coming from Python to Julia).

Best,

Abdul Naaser