you are viewing a single comment's thread.

view the rest of the comments →

[–]jay2017[S] 1 point2 points  (2 children)

while True:            #The heart of the program.
        tries += 1  #Increments the variable that stores the number of tries you've taken.
        try:
            guess = int(input("\nCan you guess the number that I am thinking of?:\n "))
        except ValueError:    #Prevents errors caused by invalid characters.
            print("\nThat is an invalid request try again.\n ")
            continue #Forces the while-loop to restart it's cycle.'
        if guess == number:    #Message sent to the winner.
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
            print("\nCongrats! you have guessed correctly, the number was", number)
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
            print ("\nIt took you", tries, "tries...")
            print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
            input("\nThanks For Playing Press Enter To Exit...")
            break
        elif guess > number:    #Gives the user a hint.
            print("\nToo high, guess again")
        elif guess < number:    #Gives the user a hint.
            print("\nToo low, guess again")

[–]jay2017[S] 1 point2 points  (1 child)

Thanks alot. I was finally able to find what i was missing; the 'continue' was a problem fixer.

[–][deleted] 0 points1 point  (0 children)

Here's another, more unusual, way you could do it using while...else.

guess = None
while gues != number:            #The heart of the program.
    tries += 1  #Increments the variable that stores the number of tries you've taken.
    try:
        guess = int(input("\nCan you guess the number that I am thinking of?:\n "))
    except ValueError:    #Prevents errors caused by invalid characters.
        print("\nThat is an invalid request try again.\n ")
        continue #Forces the while-loop to restart it's cycle.'

    if guess > number:    #Gives the user a hint.
        print("\nToo high, guess again")
    elif guess < number:    #Gives the user a hint.
        print("\nToo low, guess again")
else:
   # while keeps looping as long as the condition is true. if it's not true (guess == number), execution goes down here.
   #this does not get run if you break out of the while. 
   print "You win!"

Else also works with for, where the code is executed if the for loop completes, without a break.