-4

Write a program that asks the user to enter the number of push-ups they can do. The program should force the user to enter a valid number by repeating the question until a number between 0 and 500 is entered. Once a valid answer is provided, the program should print out: “Wimp!! I can do X.” where X is one more that what the user entered.

This is what the shells shows:

How many push-ups can you do? 1000
Liar, please enter a number between 0 and 500: 2341
Liar, please enter a number between 0 and 500: 75
Wimp!! I can do 76.
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 7
    What have you tried so far? Post your attempt – Ben Mohorc Feb 08 '18 at 14:52
  • 2
    Possible duplicate of [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) – Patrick Artner Feb 08 '18 at 14:53
  • i tried in on the python command: print " How many push-ups can you do? " pushups = float(raw_input()) while not(pushups >= "0" or pushups <= "500"): print " Liar, please enter a number between 0 and 500: " pushups = float(raw_input()) if (pushups >= "0" or pushups <= "500" ): print " Wimp!! I can do " + str(pushups) – Mikias Debesay Feb 08 '18 at 15:26

1 Answers1

-1

This should do the job:

while True:

    answer = int(input("How many push ups can you do?\n"))

    if answer > 500 or answer < 0:
        print("Liar, please enter a number between 0 and 500:")
        continue
    else:
        print("Wimp!! I can do {x}.".format(x = answer + 1))
        break
Gab
  • 512
  • 4
  • 7
  • can you write the answer in python? i dont know if thats python. thanks btw it worked – Mikias Debesay Feb 08 '18 at 15:25
  • actually that's Python 3 :) – Gab Feb 08 '18 at 15:26
  • can you look at my comment above and see what i put. Is it right? and do you know why it may be wrong? btw im running 2.7 – Mikias Debesay Feb 08 '18 at 15:43
  • i tried in on the python command: print " How many push-ups can you do? " pushups = float(raw_input()) while not(pushups >= "0" or pushups <= "500"): print " Liar, please enter a number between 0 and 500: " pushups = float(raw_input()) if (pushups >= "0" or pushups <= "500" ): print " Wimp!! I can do " + str(pushups) – Mikias Debesay Feb 08 '18 at 15:44
  • It's hard to tell if all the statements are contained in the while loop. If they are: Then nothing, not even the wimp part, will be printed when the value is in the correct range because the condition of the while loop will not be met. The condition of the while loop is only met when the number is out of range. – Gab Feb 08 '18 at 15:57