I'm new to programming so please forgive the mess below...I was trying to write a number guessing game. The computer is supposed to randomly generate a number between 1 and 10 inclusively. The user is only allowed 3 tries to correctly guess the number. One the user either guesses correctly or runs out of tries, I am supposed to have the program ask the user if they want to play again and the game is supposed to restart. Below is what I've come up with. I think I am making this much more complicated than it needs to be...what am I doing wrong because it does not work?
import random
number = random.randint(1,10)
print "The computer will generate a random number between 1 and 10. Try to guess the number!"
guess = int(raw_input("Guess a number: "))
attempts = 0
while guess != number and attempts < 4:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
else:
print "That is not an integer between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
attempts = attempts + 1
if attempts > 4:
print "You've guessed incorrectly and are out of tries..."
playAgain = raw_input("Would you like to play again? ")
if playAgain = "Yes" or playAgain == "y":
import random
number = random.randint(1,10)
attempts = 0
guess = int(raw_input("Guess a number: "))
while guess != number and attempts < 4:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
else:
print "That is not an interger between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
attempts = attempts + 1
while guess == number:
print "Congratulations, you guessed correctly!"
playAgain = raw_input("Would you like to play again? ")
if playAgain = "Yes" or playAgain == "y":
import random
number = random.randint(1,10)
attempts = 0
guess = int(raw_input("Guess a number: "))
while guess != number and attempts < 4:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
else:
print "That is not an interger between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
attempts = attempts + 1
if attempts > 3:
print "You've guessed incorrectly and are out of tries..."
playAgain = raw_input("Would you like to play again? ")
if playAgain == "yes" or playAgain == "Yes":
import random
number = random.randint(1,10)
attempts = 0
guess = int(raw_input("Guess a number: "))
while guess != number and attempts < 4:
if guess >= 1 and guess <= 10:
print "Sorry, you are wrong."
else:
print "That is not an interger between 1 and 10 (inclusive)."
guess = int(raw_input("Guess another number: "))
attempts = attempts + 1
if attempts > 3:
print "You've guessed incorrectly and are out of tries..."
playAgain = raw_input("Would you like to play again? ")
if playAgain == "yes" or playAgain == "Yes":
import random
number = random.randint(1,10)
A little less advanced solution for someone who is a beginner:
import random
attempts = 0
number = random.randint(1,10)
while attempts < 4:
attempts += 1
print number #print number to help with testing
guess = int(raw_input("Guess a number from 1 to 10: "))
if guess == number:
print "you guessed the number!",
again = raw_input("do you want to play again? y or n ")
if again == "y":
number = random.randint(1,10)
attempts = 0 # gives 4 attempts to a new game
else:
print "good bye"
break
elif attempts == 4:
print "The game is over!"
I think you must check how loops work, you cant really do things manually, check this code for an example of implementation with loops and functions:
import random
def guessGame():
guessnum = random.randint(1,10)
print "Try to guess the number between 1 and 10 in 3 ansers or less"
i = 1
while i < 4:
try:
num = int(raw_input("Attempt " + str(i) + ": "))
if num == guessnum:
return True
else:
i+=1
print "Try again"
except Exception as e:
print "Please input a valid number"
continue
def mainLoop():
while True:
guessGame()
exitopt = raw_input("Good game, wanna try again? y - yes, n - no: ")
if exitopt == "y":
continue
elif exitopt == "n":
print "Bye!!!"
break
if __name__ == "__main__":
mainLoop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.