Python 3.4
Hi guys! I need help with Boolean operators in my program. You have to guess a number set by the program, from 1 - 1000. If you guess it says good job, if not it says high/low.
Use While, if/elif/else (any of them), and also a playagain loop.
Here's what I have so far.
import random
a = int(input("I have a number between 1 and 1000. Can you guess my number?\nPlease type your first guess."))
x = random.randrange(1,1000)
counter = 0
b = False
while not b:
if a == x:
print ("Excellent! You guessed the number in", counter1,"tries.")
b = True
elif a > x:
print ("high")
counter = counter + 1
elif a < x:
print ("low")
counter = counter + 1
Your main issue was that you weren't prompting them in the loop.
import random
x = random.randrange(1,1000)
counter = 0
while True:
guess = int(input("I have a number between 1 and 1000. Can you guess my number?\nPlease type your first guess."))
if guess == x:
print ("Excellent! You guessed the number in", counter,"tries.")
break
else:
if guess > x:
print("high")
else:
print("low")
counter += 1
To prompt the user if they wish to play again, you'd do the following:
import random
x = random.randrange(1,1000)
counter = 0
while True:
guess = int(input("I have a number between 1 and 1000. Can you guess my number?\nPlease type your first guess."))
if guess == x:
print ("Excellent! You guessed the number in", counter,"tries.")
play_again = input("Type Y if you'd like to play again, or N if not")
if play_again == 'N':
break
else:
if guess > x:
print("high")
else:
print("low")
counter += 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments