It looks like you're new here. If you want to get involved, click one of these buttons!
Your program fails if the user types in something that is not number. The rest seems fine.
If someone guesses it right the first time, your program will say it only took 0 guesses.
#!/usr/bin/python
import random
'''
Random number guessing game. Just made because I was bored. :p
Number is randomly generated from between 1 and 100.
Made by nu11byte.
A lot of un-needed crap, but I was just having a play. Just refreshing
my mind. Parts of Python have been forgot and left behind for other languages syntax. Luckily Python's
syntax is quite similar to other languages.
'''
#Use and edit this as you wish.
def cls():
print '''
______ _
(_____ \ _ | |
_____) ) _| |_ | | _ ___ ____
| ____/ | | | _)| || \ / _ \| _ \
| | | |_| | |__| | | | |_| | | | |
|_| \__ |\___)_| |_|\___/|_| |_|
(____/
'''
print \"\n\"
cls()
print \"Guess what number I am thinking of. My number is between 1 and 100.\"
def main():
randomNumber = random.randint(1,100)
userGuess = 0
guessCount = 0
while (userGuess!=randomNumber):
userGuess = raw_input(\"Guess: \")
try:
userGuess = int(userGuess)
except:
print userGuess + \" is not a number.\"
main()
guessCount += 1
if (userGuess < randomNumber):
print \"Guess a little higher.\"
elif (userGuess > randomNumber):
print \"Guess a little lower.\"
elif (userGuess == randomNumber):
if guessCount == 1:
print \"You found it with the first try!\"
playagain()
else:
print \"It took you %d goes to correctly guess the number.\" % guessCount
playagain()
def playagain():
pause = raw_input(\"Play again? (y/n): \");
if (pause == \"y\"):
userGuess = 0
guessCount = 0
randomNumber = random.randint(1,100)
cls()
main()
main()
###EOF
Guess what number I am thinking of. My number is between 1 and 100.
Guess: 5
It took you 1 goes to correctly guess the number.
Play again? (y/n):
Guess what number I am thinking of. My number is between 1 and 100.
Guess: 28
You found it with the first try!
Play again? (y/n):
Guess what number I am thinking of. My number is between 1 and 100.
Guess: hi
hi is not a number.
Guess:
It says 1 not 0:Guess what number I am thinking of. My number is between 1 and 100.
Guess: 5
It took you 1 goes to correctly guess the number.
Play again? (y/n):
But with the change I made if you find it with the first try it would be like this:Guess what number I am thinking of. My number is between 1 and 100.
Guess: 28
You found it with the first try!
Play again? (y/n):
and the other change I made:Guess what number I am thinking of. My number is between 1 and 100.
Guess: hi
hi is not a number.
Guess: