-1

I've been stuck at this problem for ages, and I just don't know what to do. I'm really new to python so I don't know much and I've just been stuck at this problem.

Every time I input my answer, it just endlessly repeats itself, even though it's the right answer, I looked for help with people who had similar problems but I just don't understand. Please help. Thanks.

Here's the Code:

print ("Hello World!")
 # Name Input
name = input("What is your name sir? - ")
print ("Hello,",name)
 # To see if they put the right Response
while True:
 # The actual system to see if they got it right
    try:
        answer = int(input("Please say Y to play, or N to not play: "))
    except ValueError:
        print("Sorry please put either Y or N: ")
        continue
    else:
        break
 # if they did type the right answer, what will happen
    if answer == "Y":
      print ("Lets the game begin!")

    elif answer == "N":
      flag = False

 # Beginning of game
import random
num = random.randint(1, 20)
flag = True
 # the beginning of their guess
guess = 0
 # Start of game
print ("Can you guess my number",name, "it's between 1-20", end = " ")
 # The actual Guessing System
while flag == True:
    guess = input()
    if not guess.isdigit():
 # if they don't put the number
        print ("Sorry",name, "Please put numbers between 1-20 only. And remember, do Not put it in Word form")
        break
 # The system to see if they got the number
    elif int(guess) < num:
        print ("Sorry",name, "That's too low! Try again:", end = " ")
    elif int(guess) > num:
        print ("uhh",name, "That's too high, sorry", end = " ")
    else:
        print("That's Correct! My number is " +guess)
 # End of Game
        flag = False```

  • 2
    How do you expect Python to convert `'Y'` or `'N'` to an integer? – Matthias Nov 22 '19 at 21:45
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Ofer Sadan Nov 22 '19 at 21:46

2 Answers2

1

you should not convert input to int at line 9

print ("Hello World!")
 # Name Input
name = input("What is your name sir? - ")
print ("Hello,",name)
 # To see if they put the right Response
while True:
 # The actual system to see if they got it right
    try:
        answer = input("Please say Y to play, or N to not play: ")
    except ValueError:
        print("Sorry please put either Y or N: ")
        continue
    else:
        break
 # if they did type the right answer, what will happen
    if answer == "Y":
      print ("Lets the game begin!")

    elif answer == "N":
      flag = False

 # Beginning of game
import random
num = random.randint(1, 20)
flag = True
 # the beginning of their guess
guess = 0
 # Start of game
print ("Can you guess my number",name, "it's between 1-20", end = " ")
 # The actual Guessing System
while flag == True:
    guess = input()
    if not guess.isdigit():
 # if they don't put the number
        print ("Sorry",name, "Please put numbers between 1-20 only. And remember, do Not put it in Word form")
        break
 # The system to see if they got the number
    elif int(guess) < num:
        print ("Sorry",name, "That's too low! Try again:", end = " ")
    elif int(guess) > num:
        print ("uhh",name, "That's too high, Try again", end = " ")
    else:
        print("That's Correct! My number is " +guess)
 # End of Game
        flag = False
Yukun Li
  • 244
  • 1
  • 6
  • 1
    And we will never reach the part after `# if they did type the right answer, what will happen` in the loop. – Matthias Nov 22 '19 at 21:54
  • yes, that is why you should not have the int() at line 9 – Yukun Li Nov 22 '19 at 21:57
  • Even if you drop the `int` you will not reach that part because the program will either `continue` or `break` the loop in the lines before. – Matthias Nov 22 '19 at 22:17
  • I chek here https://repl.it/languages/python3, which code works fine – Yukun Li Nov 22 '19 at 22:23
  • I beg to differ. The program never reaches the part of the code I mentioned. You will never see "Lets the game begin!" being printed. – Matthias Nov 24 '19 at 16:31
-1

Keeping the format of the code as per the user, i have just resolved some logical errors and made a runnable program.

 # Beginning of game
import random
def game():
  num = random.randint(1, 20)
  flag = True
  # the beginning of their guess
  guess = 0
  # Start of game
  print ("Can you guess my number",name, "it's between 1-20", end = " ")
  # The actual Guessing System
  while flag == True:
      guess = input()
      if not guess.isdigit():
  # if they don't put the number
          print ("Sorry",name, "Please put numbers between 1-20 only. And remember, do Not put it in Word form")
          break
  # The system to see if they got the number
      elif int(guess) < num:
          print ("Sorry",name, "That's too low! Try again:", end = " ",)

      elif int(guess) > num:
          print ("uhh",name, "That's too high, sorry", end = " ")
      else:
          print("That's Correct! My number is " +guess)
  # End of Game
          flag = False


print ("Hello World!")
 # Name Input
name = input("What is your name sir? - ")
print ("Hello,",name)
 # To see if they put the right Response
while True:
 # The actual system to see if they got it right
    try:
        answer = (input("Please say Y to play, or N to not play: "))

 # if they did type the right answer, what will happen
        if answer == "Y" or answer =='y':
            print ("Lets the game begin!")
            game()

        elif answer == "N" or answer == 'n':
            print("Goodbye")
            flag = False
    except ValueError:
      print("Sorry please put either Y or N: ")
      continue
    else:
        break
Dhrumil Panchal
  • 406
  • 5
  • 11