Python: How to keep repeating a program until a sp

2020-01-23 12:14发布

I have a function that evaluates input, and I need to keep asking for their input and evaluating it until they enter a blank line. How can I set that up?

while input != '':
    evaluate input

I thought of using something like that, but it didn't exactly work. Any help?

4条回答
家丑人穷心不美
2楼-- · 2020-01-23 12:28

you probably want to use a separate value that tracks if the input is valid:

good_input = None
while not good_input:
     user_input = raw_input("enter the right letter : ")
     if user_input in list_of_good_values: 
        good_input = user_input
查看更多
迷人小祖宗
3楼-- · 2020-01-23 12:39

This is a small program that will keep asking an input until required input is given.

required_number = 18

while True:
    number = input("Enter the number\n")
    if number == required_number:
        print "GOT IT"
    else:
        print ("Wrong number try again")

查看更多
你好瞎i
4楼-- · 2020-01-23 12:42

There are two ways to do this. First is like this:

while True:             # Loop continuously
    inp = raw_input()   # Get the input
    if inp == "":       # If it is a blank line...
        break           # ...break the loop

The second is like this:

inp = raw_input()       # Get the input
while inp != "":        # Loop until it is a blank line
    inp = raw_input()   # Get the input again

Note that if you are on Python 3.x, you will need to replace raw_input with input.

查看更多
Lonely孤独者°
5楼-- · 2020-01-23 12:48

Easier way:

#required_number = 18

required_number=input("Insert a number: ")
while required_number != 18
    print("Oops! Something is wrong")
    required_number=input("Try again: ")
if required_number == '18'
    print("That's right!")

#continue the code
查看更多
登录 后发表回答