Post: [python][Script] Random Number Game
05-10-2011, 04:57 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); This is a simple random number game. The scripts generates a number and ask for the user to guess at a number. If the user guesses the number correctly it congratulates them, when they lose it gives them a nice message too. This is one of my first scripts as I just started Python tonight. It is quite simple using a little random number generating that I researched You must login or register to view this content.. This script is only 21 lines long and it took me less than 10 minutes to make.

I did include comments on almost every line, as I want this to be a good habit. I would like criticism and comments please.

Original Program
Here is the Code at pastebin : You must login or register to view this content.

And here is the code in
     tags.

[code]
#!/usr/bin/python
import time #Importing the Time module.
import random #Importing the Random module.
x = random.randint(1, 5) #Defines the "x" variable as a random integer between 1 and 5, including 1 and 5.
print "Hello and Welcome to the Random Number Game"
print "Made by Killoy at HackForums.net"
print " "
time.sleep(2) #Telling the program to pause for a few seconds for the user to read the displayed text.
y = int(raw_input("Enter a Number between 1 and 5: ")) #Lets the user define the "y" variable by using a raw input function.
time.sleep(1) #Pausing again.
while True: #Starts a simple loop.
if x == y: #Checks to see if the random variable "x" is equal to the user defined variable "y".
print "Congratulations, You Win!" #Tells the user that because variable "x" is equal to variable "y" then they win.
time.sleep(3) #Pauses to let the user see that they won.
break #Stoping the loop and jumping to after the loop.
else: #If variable "x" does not equal variable "y".
print "Try Again :(" #Tells the user to try again.
time.sleep(3) #Pauses to let the user see that they have lost.
break #Stoping the loop and jumping to after the loop.
print "Thanks for playing!" #Tells the user thanks for playing.
time.sleep(3) #Pauses to let the user see that the game is over.

[align=center]
--
[/align]
[align=center]UPDATE 1[/align]

I added in the ability to try again without running the program again thanks to Hyper_Shado! I also removed the sleeps as they were deemed unnecessary.

PasteBin: You must login or register to view this content.

    #!/usr/bin/python
import time #Importing the Time module.
import random #Importing the Random module.
x = random.randint(1, 5) #Defines the "x" variable as a random integer between 1 and 5, including 1 and 5.
print "Hello and Welcome to the Random Number Game"
print "Made by Killoy at HackForums.net"
print " "
y = int(raw_input("Enter a Number between 1 and 5: ")) #Lets the user define the "y" variable by using a raw input function.
while True: #Starts a simple loop.
if x == y: #Checks to see if the random variable "x" is equal to the user defined variable "y".
print "Congratulations, You Win!" #Tells the user that because variable "x" is equal to variable "y" then they win.
break #Stoping the loop and jumping to after the loop.
else: #If variable "x" does not equal variable "y".
y = int(raw_input("Try again!: ")) #Let's the user try again.
print "Thanks for playing!" #Tells the user thanks for playing.
time.sleep(3) #Pauses to let the user see that the game is over.


Thanks, remember comments and criticism please!

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo