0
from random import randint
x=(randint(0,9))
print "I'm thinking of a number between 1 and 10."
y = raw_input("What is your number? (Integer from 1 to 10)")
if y<x:
    print "Too low!"
elif y>x:
    print "Too high!"
elif y==x:
    print "Spot On!"
    sys.exit()

How do I loop it so you have to keep guessing until you get the number?

  • Related: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – NightShadeQueen Aug 12 '15 at 04:19

2 Answers2

0

You might want to investigate the while loop for that purpose. Please check the docs for details and check answers already in place for helpful code sniplets.

Community
  • 1
  • 1
flaschbier
  • 3,910
  • 2
  • 24
  • 36
0

Just break when you get the correct number

from random import randint
x=(randint(0,10))
print "I'm thinking of a number between 1 and 10."
while True:
    y = int(raw_input("What is your number? (Integer from 1 to 10)"))
    if y<x:
        print "Too low!"
        print "Let's try again"
    elif y>x:
        print "Too high!"
        print "Let's try again"
    elif y==x:
        print "Spot On!"
        break

Converted y to int and if you want to include 10 then you must iterate till 10 not 9 in randint function

Shrey
  • 1,242
  • 1
  • 13
  • 27