Difference between revisions of "SPR720 Python Lab I"

From CDOT Wiki
Jump to: navigation, search
(Initial text.)
 
(Changed the variable 'input' to 'guess')
 
Line 6: Line 6:
 
  secret=random.randrange(1,100)
 
  secret=random.randrange(1,100)
 
   
 
   
  input=int(raw_input("Enter a guess: "))
+
  guess=int(raw_input("Enter a guess: "))
 
   
 
   
  if input<secret:
+
  if guess<secret:
 
  print "Too low!"
 
  print "Too low!"
  elif input>secret:
+
  elif guess>secret:
 
  print "Too high!"
 
  print "Too high!"
 
  else:
 
  else:

Latest revision as of 16:24, 13 November 2008

Here is a small Python script:

#!/usr/bin/python

import random
secret=random.randrange(1,100)

guess=int(raw_input("Enter a guess: "))

if guess<secret:
	print "Too low!"
elif guess>secret:
	print "Too high!"
else:
	print "Correct!"
  1. Save this script, set the permissions correctly, and run it.
  2. Modify the script so that the user is given playing instructions before the game starts.
  3. Make the script loop to prompt for input until the the correct number is guessed.
  4. Displays the number of guesses the user took to guess the secret (score).
  5. Modify the script so that it does not stop when invalid data is entered (such as a word). Suggestion: use try/except.
  6. Accept only input value from 1 to 100.
  7. Write a blog entry giving your impressions of Python.