For example, if the user enters an interger out of the range of (1, 100) it will print a friendly message and ask the user to re-enter a new number. So far, the code I made for this will ignore the restrictions I placed in and just tell the user if the guess is too high or low.
Also, when a user enters a string, the game will tell the user the answer is invalid and tell the user to re-enter a new number. I got my code to work for this until I enter in another string character:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
main()
File "C:\Users\Mary Anne\Desktop\Cmpt 120\Assignments\a3.py.py", line 51, in main
guess = int(raw_input("Please re-enter your guess: "))
ValueError: invalid literal for int() with base 10: 'as'
This is my code so far...
import random
def main():
name = raw_input("Hi! What's your name? ") #Tells the user to enters his/her name.
if name == name:
print 'Nice to meet you ' + name + '!' #Prints a personalized message with instructions.
print "Let's play a game.\n"
print "I am thinking of a number from 1 to 100."
print "You try to guess the number. If you guess right, you win!"
print "If you guess wrong, I will tell you if your guess is too low or too high.\n"
print "Got it?"
print "Good! Let's play!"
play = 'y'
while play == 'y': #When user chooses y, the game is restarted in while loop.
secret_number = (random.randint(1, 100)) #Secret number is randomly chosen through 1 - 100.
guess_count = 0 #Guess count starts from 0.
try:
guess = int(raw_input("\nWhat is your first guess? ")) #User enters his/her first guess.
while guess != secret_number: #A while loop is created if the user does not get the secret number right.
print "That's not it " + name + "." #Prints out a personalized message.
if guess < secret_number:
guess_count += 1 #Adds one to the guess count.
print " Your guess is too LOW." #Prints this message out if the guess is lower than the secret number.
guess = int(raw_input("\nWhat is your next guess? ")) #Tells the user to enter in the next guess.
if guess > secret_number:
guess_count += 1 #Adds one to the guess count.
print " Your guess is too HIGH." #Prints this message out if the guess is higher than the secret number.
guess = int(raw_input("\nWhat is your next guess? ")) #Tells the user to enter in the next guess.
if guess < 1 or guess > 100:
print "\nOops: that's not a valid guess."
guess = int(raw_input("Please re-enter your guess: "))
except ValueError:
print "\nOops: that's not a valid guess."
guess = int(raw_input("Please re-enter your guess: "))
if guess == secret_number:
if guess_count == 0: #If user got the secret number on the first try, this special message is produced.
guess_count += 1 #Adds one to the guess count.
print "Amazing: You got in on your first try!"
print "It took you " + str(guess_count) + " guess(es)" #Tells the user how many guesses he/she made.
if 1 < guess_count < 10: #If the user guesses the secret number under 10 guesses, this special message is produced.
guess_count += 1 #Adds one to the guess count.
print "Nice work!"
print "It took you " + str(guess_count) + " guess(es)" #Tells the user how many guesses he/she made.
if guess_count > 10: #If the user guesses the secret number over 10 tries, no special message is produced.
guess_count += 1 #Adds one to the guess count.
print "It took you " + str(guess_count) + " guess(es)" #Tells the user how many guesses he/she made
play = str(raw_input("\n\nWanna play again(y/n)? ")) # When user picks y, the game starts over.
# When user picks n, the game ends.
Any help would be great, thanks!

New Topic/Question
Reply




MultiQuote




|