I have recently started learning Python from an online course and we had this course assignment to program this game. I am avoiding the name of the course and the name of the online resource on purpose because I don't want this show up on Google results. I'm just childishly happy that I wrote something and I wanted to save it here. This code may not work as is, because it uses some modules that are not part of standard python installations. After writing it in Python, I tried to do it on Powershell too and it kind of worked.
import simplegui import random # initialize global variables secret_number = random.randrange(0,100) maximum_guesses = 7 def range100(): """Button that changes range to range(0,100)""" global secret_number global maximum_guesses secret_number = random.randrange(0,100) maximum_guesses = 7 print "New Game! Enter a guess between 0 and 100. \n" def range1000(): """button that changes range to range(0,1000)""" global secret_number global maximum_guesses secret_number = random.randrange(0,1000) maximum_guesses = 10 print "\nNew Game! Enter a guess between 0 and 1000. \n" def get_input(guess): global maximum_guesses global secrent_number maximum_guesses -= 1 guessed_number = float(guess) if guessed_number == secret_number: print "Your guess", guess, "is correct! YOU WIN!" print "You had", maximum_guesses, "guesses left.\n" range100() elif maximum_guesses > 0: if guessed_number > secret_number: print guess+"?","incorrect! try LOWER.", maximum_guesses, "guess(es) left. \n" if guessed_number < secret_number: print guess+"?","incorrect! try HIGHER.",maximum_guesses, "guess(es) left. \n" else: print "Your guess", guess, "is incorrect. You lose. Why not try again!\n" range100() frame = simplegui.create_frame("Guess The Number", 200, 200, 200) frame.add_input("Enter your guess and hit enter key", get_input, 200) frame.add_button("Change range to 100", range100) frame.add_button("Change range to 1000", range1000) frame.start()Now lets try this on Powershell
$random = New-Object system.random $RandomNumber = $random.next(1,100) do { $guess = read-host -prompt "What is your guess?" if ($guess -gt $RandomNumber) {Write-Host "Incorrect! Try Lower!"} elseif ($guess -lt $RandomNumber) {Write-Host "Incorrect! Try Higher!"} elseif ($guess -eq $RandomNumber) {Write-Host "That is CORRECT! You Rock! It took you $count tries"} else {"You have to guess a number!!!"} $count += 1 } while ($guess -ne $RandomNumber)
No comments:
Post a Comment