Reputation: 25
This question is difficult to explain, I hope that title doesn't confuse people.
So I have to make the user guess a number from 1 - 100, and if the user guesses the number correctly, I want it to restart the game but generate a new number and display the welcome message again. Also the user can only try 10 times and then have a game over message appear. I am stuck trying to display the new message, generate a new number, have the number be the same throughout the loop and fail the user at 10 tries.
I am sorry again if this is confusing.
This is what I have:
import random
import time
def getInput():
x = random.randrange(100)
print(x) # for testing
print("***Welcome! Guess and try to find a number between 1 and 100!***")
while True:
userGuess = int(input("Enter Your Guess: "))
if (userGuess > x):
print("Lower! Enter Again: ")
elif (userGuess < x):
print("Higher! Enter Again: ")
elif (userGuess > 100):
print("Guess must be between 1 and 100")
elif (userGuess < 1):
print("Guess must be greater then 0")
elif (userGuess == x):
print("You win!")
time.sleep(3)
continue
def main():
getInput()
main()
Upvotes: 1
Views: 659
Reputation: 1898
Here if one more way of doing it:
import random
import time
def matchGuess(randNumber, userGuess):
if userGuess > 100 : print "Guest must be between 1 and 100"
elif userGuess < 0 : print "Guest must be greater than 0"
elif userGuess > randNumber : print "Lower! Enter Again:"
elif userGuess < randNumber : print "Higher! Enter Again:"
elif userGuess == randNumber : print "You win!"
if __name__ == "__main__":
print "***Welcome! Guess and try to find a number between 1 and 100!***"
for i in range(10):
userGuess = int(raw_input("Enter Your Guess:"))
randNumber = random.randrange(100)
matchGuess(randNumber, userGuess)
Upvotes: 0
Reputation: 880
I tried to keep the changes to minimum for you to easily understand, see @Tom's answer for a better way
import random
import time
def startNewGame():
x = random.randrange(100)
print(x) # for testing
print("***Welcome! Guess and try to find a number between 1 and 100!***")
numberOfTries = 0
while numberOfTries<10:
userGuess = int(input("Enter Your Guess: "))
numberOfTries += 1
if (userGuess > x):
print("Lower! Enter Again: ")
elif (userGuess < x):
print("Higher! Enter Again: ")
elif (userGuess > 100):
print("Guess must be between 1 and 100")
elif (userGuess < 1):
print("Guess must be greater then 0")
elif (userGuess == x):
print("You win!")
time.sleep(3)
return
print("You Lose!")
time.sleep(3)
def main():
startNewGame()
while(True):
again = input("Would you like to play again?(yes/no) ")
if again == "yes":
startNewGame()
elif again == "no":
break
main()
Upvotes: 1
Reputation: 350760
Here is how I would suggest to do it. I added comments with three hashes where I made changes:
import random
import time
def playMany():
def play():
x = random.randrange(100)
print(x) # for testing
print("***Welcome! Guess and try to find a number between 1 and 100!***")
for attempt in range(10): ### try 10 times at the most
userGuess = int(input("Enter Your Guess: "))
### change the order of the tests:
if (userGuess > 100):
print("Guess must be between 1 and 100")
elif (userGuess < 1):
print("Guess must be greater then 0")
elif (userGuess > x):
print("Lower!") ### Don't ask to "enter" here
elif (userGuess < x):
print("Higher!")
else: ### no need to test for equality: it is the only possibility left
print("You win!")
return True ### Exit play() with True (to play again)
### Deal with too many wrong guesses
print('Too many wrong guesses. Game over')
return False
while play(): ### Keep repeating the game until False is returned
time.sleep(3) ### Move sleep here
def main():
playMany() ### Use a more telling function name
main()
See it run on repl.it.
Upvotes: 0
Reputation: 5349
Each time the loop starts you need to generate a new random number then reset the number of tries to 0
import random
import time
def getInput():
x = random.randrange(100)
print(x) # for testing
print("***Welcome! Guess and try to find a number between 1 and 100!***")
tries = 0 # we need a variabe to see how many tries the user has had
while True:
userGuess = int(input("Try "+str(tries + 1)+" Enter Your Guess: "))
if (userGuess == x):
print("You win!")
print("***Think you can win again? Guess and try to find a number between 1 and 100!***")
x = random.randrange(100)
tries = 0 # reset tries
print(x) # we need a new random number for the user to guess
time.sleep(3)
continue
elif (userGuess > x):
print("Lower! Enter Again: ")
elif (userGuess < x):
print("Higher! Enter Again: ")
if (userGuess > 100):
print("Guess must be between 1 and 100")
elif (userGuess < 1):
print("Guess must be greater then 0")
else:
tries = tries + 1 # add 1 to tries unless they make an invalid guess
if tries == 10:
print("<<GAME OVER>>")
break # end
def main():
getInput()
main()
Upvotes: 1
Reputation: 1533
def getInput():
for i in range(10):
#loop body
return "Game Over."
If I'm interpreting you want to end after 10 guesses.
Upvotes: 1