none
PowerShell: Problems on second execution RRS feed

  • Question

  • Hey Guys,

    I'm new to this forum and I'm currently learning PowerShell. As I already have programming knowledge I just did a bit of "learning by doing" to get familiar with the language. And after the first steps I tried to code one of the main example programs, just a little game where the player has to guess a random number between 1 and 100. He got five tries and the program answers wether his guess is to low or to high. So far so bad. There is one thing that don't really works and I just don't have any explanation for it, but first the source code:

    "Let's play a game about numbers."
    "I got a number between 1 and 100 and you have to tell me which number it is! You have 5 trys."
    $mynumber = get-Random -Minimum 1 -Maximum 101
    $trysleft = 4
    $input = 0
    while((($input = Read-Host -Prompt "What do you think?") -ne $mynumber) -and ($trysleft -gt 0)) {
        if ($input -gt $mynumber) {
            "To high!"
        } else {
            "To low!"
        }
        $trysleft--
    }
    
    if($input -eq $mynumber) {
        "You got it!"
    } else {
        "Failed..."
        "My number was " + $mynumber
    }

    It seems that it works pretty well when first executed in the ISE but when I execute it another time the output is just wrong (50 --> To low; 100 --> To low; 150 --> To low). But as I think all variables are manualy initialized/resetted in the beginning of the program and thats why there is no reasen for me why something should be different at the second execution.

    May anyone help me with that?

    Thanks in advance!

    Benno



    • Edited by c0ding420 Monday, April 18, 2016 3:31 PM Unclear Title
    Monday, April 18, 2016 3:24 PM

Answers

  • First your logic is a bit flakey.  You also cannot use $input since it is a reserved variable.

    This is what you are trying to do:

    $mynumber = get-Random -Minimum 1 -Maximum 101
    $attempts=0
    do{
    	$x = [int](Read-Host -Prompt "What do you think?")
    	if ($x -eq $mynumber) {
    		"You got it!"
    		break
    	}elseif($x -gt $mynumber) {
    		'Too high'
    	}else{
    		'Too low'
        }
    	$attempts++
    }until($attempts -eq 4)
    
    It should work under all circumstances.


    \_(ツ)_/

    • Marked as answer by c0ding420 Tuesday, April 19, 2016 12:09 PM
    Monday, April 18, 2016 6:26 PM

All replies

  • You are running into scope problems.

    If you just type the code into the editor and use F5 to run it, the variables will all be in the current scope.

    One fix is to save your code as a script file (something.ps1), and run the script instead of using the F5 key.


    -- Bill Stewart [Bill_Stewart]

    Monday, April 18, 2016 4:20 PM
    Moderator
  • First your logic is a bit flakey.  You also cannot use $input since it is a reserved variable.

    This is what you are trying to do:

    $mynumber = get-Random -Minimum 1 -Maximum 101
    $attempts=0
    do{
    	$x = [int](Read-Host -Prompt "What do you think?")
    	if ($x -eq $mynumber) {
    		"You got it!"
    		break
    	}elseif($x -gt $mynumber) {
    		'Too high'
    	}else{
    		'Too low'
        }
    	$attempts++
    }until($attempts -eq 4)
    
    It should work under all circumstances.


    \_(ツ)_/

    • Marked as answer by c0ding420 Tuesday, April 19, 2016 12:09 PM
    Monday, April 18, 2016 6:26 PM
  • Thank you!
    Tuesday, April 19, 2016 12:10 PM
  • Also thanks for your answer even if it wasn't the one that really helped me! :)
    Tuesday, April 19, 2016 12:11 PM
  • I've also written a game much like this one. My version keeps track of the number of attempts, the number of games played, and therefore, the average attempts per game. That last one is the one I thought you might like to know about.

    After having played this a ton of times -- when testing, when bored, when patching servers -- I can firmly say that the average attempts is around seven. I suspect your users are going to stop playing after a round or two, whereas they might play longer if they have more tries, or actually have an option to finally guess the number. In mine, there's no limit to the number of guesses. Once they guess it, the game's over and the attempts, etc. are calculated and presented. Here's the link, if you're interested: https://gallery.technet.microsoft.com/The-1-to-100-Game-5288e279. The link includes an image that shows the output when playing.


    Wednesday, April 20, 2016 6:36 PM