-2

I wrote a script that works, but when I open it as a .py file email attachment, after I finish answer the inputs and it runs, it closes. What can I do to stop this. Here is the code if it helps:

# Write a program to solve an ancient Chinese puzzle: We count 35 heads
# and 94 legs among the chickens and rabbits on a farm. How many rabbits
# and how many chickens are there?


# Inputs the number of heads and legs, respectively.
# Makes sure it is read as an integer.

h=input('Enter the number of heads:')
heads=int(h)
le=input('Enter the number of legs:')
legs=int(le)

# Executes a while loop while the total number of rabbits is less
# than or equal to 35.

rabbits=0
while rabbits <= heads:
    chickens = heads - rabbits

# Prints the result if there are 35 heads and 94 legs found.

    if 2 * chickens + 4 * rabbits == legs and chickens + rabbits == heads:
        print('There are', rabbits, 'rabbits and',  chickens, 'chickens.')
        break
    rabbits+=1



if 2 * chickens + 4 * rabbits != legs or chickens + rabbits != heads:
    print('No answer.')   
Michael Shore
  • 95
  • 2
  • 7

1 Answers1

1

You could easily just put input() at the end of your script to keep it from terminating.

As you may know, input() takes in a string from the standard input and then you can do with it whatever you want which in this situation isn't necessary.

Codenzi
  • 26
  • 3