1

Just making a silly program to work on my Powershell skills. The outer do...while works. I have even gotten the inner do...while to work, however instead of breaking both loops when the inner loop has been satisfied, it goes back to "Are you in a hurry? y/n:" I have tried moving the break(s) around to various different spots in the code and I have tried labels. Nothing seems to give me the results I am looking for. I understand there are probably ways a million times better to write this code and eventually I will look for ways to optimize. Right now, I am just trying to write the code in the way my mind works and to solve the problem - that is to say, no code rewrites please... unless what I am trying to do is impossible.

$resultArray = @("go", "not go")

# give result
$result = $resultArray | Get-Random

# text results to jason

# check if in hurry

do{
while ($get_in_hurry = read-host "Are you in a hurry? y/n:"){

# if in hurry, jump to spin
if ($get_in_hurry -eq "y") {
    Write-Host "You seleted you are in a hurry."
    Write-Host "My suggestion is to" $result "to poker with Jason tonight."
    break
}

# if not in hurry ask questions
elseif ($get_in_hurry -eq "n") {
    Write-Host "You seleted you are NOT in a hurry."
    # check mood
    do{
    while ($get_mood = read-host "Are you in a good mood? y/n:") {

    # if not in good mood, adjust percentage, and spin
    if ($get_mood -eq "n") {
        Write-Host "You seleted you are not in a good mood."
        Write-Host "Write something funny"
        $resultArray += "not go", "not go", "not go", "not go", "not go", "not go", "not go", "not go"
        Write-Host "My suggestion is to" $result "to poker with Jason tonight."
        break
    }

    # good mood, adjust percentage
    elseif ($get_mood -eq "y") {
        Write-Host "You seleted you are in a good mood."
        break
    }

    # if mood not yes or no, request yes or no
    else {
        Write-Host "Please speak in english. Are in a good mood? y/n:"
    }

    }

# gone to poker in last month, adjust percentage
# jason ask to go in last two weeks, adjust percentage
# did you go, adjust percentage
# do math

    }
    while (($get_mood -eq $null) -or ($get_mood -eq ""))
    }


# if hurry not yes or no, request yes or no
else {
    Write-Host "Please speak in english. Are in a hurry. y/n:"
    # save variable text to jason to update application to give better response to any funny answers that may be given
}
}


}
# if hurry null, request yes or no
while (($get_in_hurry -eq $null) -or ($get_in_hurry -eq ""))
5
  • 1
    Just a quick overlook ... you use a variable definition as condition in your 2nd and 3rd while condition. That's not going to work as you may expect it to. 😉 Commented Oct 5, 2023 at 13:52
  • Try this to see what I mean: if($get_mood = read-host "Are you in a good mood? y/n:"){'yes'}else{'no'} Commented Oct 5, 2023 at 13:55
  • I started with just the "hurry" loop. Once I got that working as expected (prompting again on null & non 'y' / 'n' responses and doing math/breaking the loop, aka stopping the application in this case, on 'y' / 'n' responses), I then nested the "mood" loop under a 'n' response. I tried using the same formula I used for the "hurry" loop, since it seemed to work. I want to try your suggestion. Not quite sure where you want me to insert it and/or what I would need to comment out. Commented Oct 5, 2023 at 14:24
  • The idea is to just keep nesting the next question under the "correct" responses. I am sure once I put together what you are trying to show me I'll see this approach is not feasible, but right now I am not making the connection. Appreciate all of your help! Commented Oct 5, 2023 at 14:29
  • Do not insert the snippet I suggested anywhere!!!! Run it as it is and answer once with y and once with n. Then you may get what I mean. Commented Oct 5, 2023 at 15:37

1 Answer 1

1

PowerShell has a little-used feature that allows you to label loops and then jump to those labels from nested loops to either break out of (break) or resume (continue) the target loop.

Here's a simplified example (using just one label, but you're not limited to that):

:toplevel while ($get_in_hurry = Read-Host "Enter something (just Enter exits)") {
  while ($get_mood = Read-Host "Back to outer loop (y) or exit (n)?") {
    if ($get_mood -eq 'y') { 
      continue toplevel # *Resume* (continue with) the outer loop.
    } else {
      break toplevel    # *Break out of* the outer loop.
    }
    Write-Warning 'Invalid input. Please try again.'
  }
}

'Continuing after top-level loop.'

Note that even though the label is defined with a leading : (e.g. :toplevel), on referencing it the leading : must be omitted (e.g. break toplevel) - if you accidentally use : (e.g.
break :toplevel), the label is ignored; allowing optional use of : to avoid this pitfall is the subject of GitHub issue #5724.

See also the docs:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.