提示输入范围中的值

2024-04-18 23:17:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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.

这是贝壳所展示的:

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.

Tags: andthetonumberthatisbetweenprogram
1条回答
网友
1楼 · 发布于 2024-04-18 23:17:23

这应该可以做到:

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

相关问题 更多 >