-1
import random


print("Hey there, player! Welcome to Emily's number-guessing game! ")
name=input("What's your name, player? ")

random_integer=random.randint(1,25)

tries=0
tries_remaining=10

while tries < 10:
  guess = input("Try to guess what random integer I'm thinking of, {}!   ".format(name))
  tries += 1
  tries_remaining -= 1

# The next two small blocks of code are the problem.

  try:
    guess_num = int(guess)
  except:
    print("That's not a whole number!   ")
    tries-=1
    tries_remaining+=1


  if not guess_num > 0 or not guess_num < 26:
    print("Sorry, try again! That is not an integer between 1 and 25!   ")
    break



  elif guess_num == random_integer:
    print("Nice job, you guessed the right number in {} tries!   ".format(tries))
    break


  elif guess_num < random_integer:
    if tries_remaining > 0:
      print("Sorry, try again! The integer you chose is a litte too low! You have {} tries remaining.   ".format(int(tries_remaining)))
      continue
    else:
      print("Sorry, but the integer I was thinking of was {}!   ".format(random_integer))
      print("Oh no, looks like you've run out of tries!   ")



  elif guess_num > random_integer:
    if tries_remaining > 0:
      print("Sorry, try again! The integer you chose is a little too high. You have {} tries remaining.   ".format(int(tries_remaining)))
      continue
    else:
      print("Sorry, but the integer I was thinking of was {}!   ".format(random_integer))
      print("Oh no, looks like you've run out of tries!   ")

I'll try to explain this as well as I can... I'm trying to make the problem area allow input for guesses again after the user inputs anything other than an integer between 1 and 25, but I can't figure out how to. And how can I make it so that the user can choose to restart the program after they've won or loss?

Edit: Please not that I have no else statements in the problems, as there is no opposite output.

2 Answers2

1

Use a function.Put everything in a function and call the function again if the user wants to try again! This will restart the complete process again!This could also be done if the user wants to restart. Calling the method again is a good plan.Enclose the complete thing in a method/function.

Mathews Mathai
  • 1,707
  • 13
  • 31
0

This will solve the wrong interval

 if not guess_num > 0 or not guess_num < 26:
    print("Sorry, try again! That is not an integer between 1 and 25!   ")
    continue

For the rest, you can do something like this

create a method and stick in your game data

def game():
   ... 
   return True if the user wants to play again (you have to ask him)
   return False otherwise

play = True
while play:
   play = game()
Simone Zandara
  • 9,401
  • 2
  • 19
  • 26