Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to fix Loop python Guess the Number game

Zelandini Borromeu Guterres:

Firstly i'm working on a simple project which is creating "Guess The Number Game". I managed bypass few code problems, but there is one thing which I can't go through it, so I need your help.

I made a loop to check if the input is a number, and inside this loop there is another loop which is checking the player's answer to restart the game is alphabetic or not.

There is no error in the code but only misunderstandment. So let's play the game and I ending up loosing the game and the program asked me if I want to play again or not, either I answer yes or no, the output will Print "That's not numeric" and then it will restart the game if I answered 'yes' or stop the game if I answered 'no', this string "That's not numeric" meant to print when I try to guess the number but I didn't type a number.

Can anyone fix the code and explain to me what was the problem?

Thank You, Zelandini

**In this code go to def play_again and analyze the loop pattern. Remember to look up the "EXCEPT" part, because that is causing it! **


import random
import time


def main():
print('Hello there what is your name?')
USER = input('>')

print('Hello, ' + USER + '')
print('Do you want to play a game? [Yes/No]')


while True:
question = input('>')
print(" ")

if question and question.isalpha():

if question == 'n' or question == 'No' or question == 'no' or question == 'N' or question == 'NO':
print('Bye, ' + USER + '!')

if question == 'y' or question == 'Yes' or question == 'Y' or question == 'yes':
print("Okay, the game is to guess my number,")
play_again()

break

elif question and question.isdigit():
print("Name must be Alphabet characters only!")

else:
print("Please enter something")



def play_again():

NumOfTry = 4
number = random.randint(1, 10)

if NumOfTry != 0:
play_again =True

print("try to guess a number between 1 and 10 in 4 tries,")
print(" ")

while play_again == True:

try:
print("please enter a number between 1 and 10:")
Guess = int(input(">"))

if Guess > number:

print(Guess, "is too high")
NumOfTry = NumOfTry - 1
if NumOfTry > 1:
print(NumOfTry, "attempts left")
print("")

if NumOfTry print(NumOfTry, "attempt left")
print("")

elif Guess
print(Guess, "is too low")
NumOfTry = NumOfTry - 1
if NumOfTry > 1:
print(NumOfTry, "attempts left")
print("")

if NumOfTry print(NumOfTry, "attempt left")
print("")

elif Guess == number:

print("You Win, Congratulations!!!")
print("Would you like to play again?")

while True:
response = str(input('>')).lower()

if response and response.isalpha():
if response in ("yes, y"):
print("Restarting")
time.sleep(1)
print(" ")

play_again()

if response in ("no, n"):
print("Okay bye")
play_again = False

play_again()

break

elif response and response.isdigit():
print("Name must be Alphabet characters only!")
else:
print("Please enter something")


if NumOfTry == 0:
print("You lose, You have out of attempts,")
print("Would you like to play again?")

while True:
response = str(input('>')).lower()

if response and response.isalpha():
if response in ("yes, y"):
print("Restarting")
time.sleep(1)
print(" ")

play_again()

if response in ("no, n"):
print("Okay bye")
play_again = False

play_again()


elif response and response.isdigit():
print("Name must be Alphabet characters only!")

except:
print(" ")
print("That's not numeric.")
print("Try again....")
print(" ")


if __name__ == '__play_again__':
play_again()

if __name__ == '__main__':
main()



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to fix Loop python Guess the Number game

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×