# Function does not work as expected

**URL:** https://discourse.julialang.org/t/function-does-not-work-as-expected/27449
**Category:** New to Julia
**Created:** [August 12, 2019, 11:24am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449 "2019-08-12T11:24:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![abdul-naaser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdul-naaser/32/9745_2.png) [@abdul-naaser](https://discourse.julialang.org/u/abdul-naaser)
#### Post date: [August 12, 2019, 11:24am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/1 "2019-08-12T11:24:46Z")

</div>

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.

```julia
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

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 12, 2019, 11:38am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/2 "2019-08-12T11:38:16Z")

</div>

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

```julia
if roll == 7 || roll == 11

```

and

```julia
elseif roll == 2 || roll == 3 || roll == 12

```

But after this, the function

```julia
diceroll()

```

is not defined for me, but probably for you.

In general, consider the following:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 12, 2019, 11:41am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/3 "2019-08-12T11:41:46Z")

</div>

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]`

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 12, 2019, 11:42am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/4 "2019-08-12T11:42:51Z")

</div>

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

I have opend an issue:  
[https://github.com/JuliaLang/julia/issues/32871](https://github.com/JuliaLang/julia/issues/32871)

---

<div class="post-metadata">

### Author: ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)
#### Post date: [August 12, 2019, 1:11pm UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/5 "2019-08-12T13:11:48Z")

</div>

They’re listed under control flow:

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

---

<div class="post-metadata">

### Author: ![abdul-naaser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdul-naaser/32/9745_2.png) [@abdul-naaser](https://discourse.julialang.org/u/abdul-naaser)
#### Post date: [August 14, 2019, 8:09am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/6 "2019-08-14T08:09:42Z")

</div>

Thank you very much!

Your answer works!

Best,

Abdul Naaser

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

```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

```

---

<div class="post-metadata">

### Author: ![abdul-naaser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abdul-naaser/32/9745_2.png) [@abdul-naaser](https://discourse.julialang.org/u/abdul-naaser)
#### Post date: [August 14, 2019, 8:10am UTC](https://discourse.julialang.org/t/function-does-not-work-as-expected/27449/7 "2019-08-14T08:10:28Z")

</div>

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

Best,

Abdul Naaser
