Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with this script

Status
Not open for further replies.

Cheesywillie

Programmer
Nov 6, 2005
1
0
0
US
I am a Web designer but i am learning python. I am reading a tutorial and I came to the guess the number seciton could some1 answer me 2 questions about the game code:
How do i get a random Number to be generated?
How do i get the game to end after 20 guesses?

Here is my code thus far:
number = 78
guess = 0

while guess != number :
guess = input ("Guess a number: ")

if guess > number :
print "Too high"

elif guess < number :
print "Too low"

print "Just right"

Thanx for any Help
 
import random, sys
number = random.randint(1,100) # Random number between 1 and 100
guess = 0
failed = 0

while guess != number :
guess = input ("Guess a number: ")

if guess > number :
print "Too high"
failed = failed + 1
elif guess < number :
print "Too low"
failed = failed + 1
if failed == 20:
print "Sorry"
sys.exit()
print "Just right"



&quot;If you always do what you've always done, you will always be where you've always been.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top