用户输入和while循环

时间:2013-10-27 19:25:30

标签: python loops input while-loop

我正在尝试编写一个Python程序,要求用户使用1,2或3进行选择。如果用户没有输入,则会提示用户只输入这些数字。

一旦用户输入1,2或3,程序就会再次要求输入1,2或3.这会重复10次,如果用户没有输入1,2或3,则会提示用户只输入那些数字。这是我到目前为止所拥有的;

while(choice>3 or choice<1):
    choice = int(input("Please enter a value from 1 - 3 only:" ))
    while (((choice == 1 or choice == 2 or choice == 3) and (count < 10))):
            run program

问题是,如果用户最初输入1,2或3,则程序不会运行。但是如果用户第一次输入1,2或3以外的东西,程序的行为就像我想要的那样。

4 个答案:

答案 0 :(得分:1)

输入1,2,3时不会运行,因为while循环会排除这些数字。

代码:

while(choice>3 or choice<1):

1,2, and 3将跳过此块 - 因此块内的代码将不会运行,程序将不执行任何操作。

答案 1 :(得分:1)

使用与您所拥有的代码尽可能相似的代码:

try_count = 0
choice = ""
valid = False

while True:
    print("Please enter a number between 1 and 3")
    while True:
        try_count += 1

        choice = input("")

        try: #make sure the user has entered a number
            choice = int(choice)
            if choice >= 1 or choice <= 3: #if the value is outside our range
                valid = True
                break #then we're done!
        except:
            continue #if the user didn't enter a number, go around again

        if try_count >= 10: #if we've done this ten times exit
            try_count = 0
            break #exit to the outer loop

        if valid: #if we've got a proper value, we're done
            break #exit the loop

答案 2 :(得分:0)

就像上面提到的Blue Ice一样,问题在于这一行:

while(choice>3 or choice<1):

问题是,如果选择大于3或小于1,则只运行此循环,但在此循环内,您正在测试此条件:

while (((choice == 1 or choice == 2 or choice == 3) and (count < 10))):

此代码始终无法访问,因为第一个while循环运行它需要1,2或3以外的数字才能进入循环。这意味着这个while循环永远不能运行,因为你要测试的第一个条件是伪造的,因此你和比较永远不能评估为真,因为True and False is False无论什么

我个人不会以你的方式创建我的输入提示,因为它不像其他选项那样容易维护。可以使用字典在python中创建菜单,这是我喜欢的方式。您所做的是将键作为选项存储,并将值作为对要执行的函数的引用。我更喜欢这个,因为它消除了在循环中设置所有布尔测试的需要,它使得选项的添加或删除更容易。

创建像你要求的菜单的代码看起来像这样。

options = {     1 : option_1,
                2 : option_2,
                3 : option_3
}

user_input = int(raw_input("1, 2, or 3"))

#The () are required because otherwise you will get the memory address of the function and the funciton itself won't run
options[user_input]()

这当然意味着您必须将代码放在一个函数中(为此,语法看起来像这样)

def function():
    do stuff

注意字典中如何在没有()的情况下存储函数。这样做是因为没有括号它是对函数本身的引用(如果你执行打印,你会看到它给你内存地址),函数将执行括号。然后,您只需通过将括号添加到二进制查找(如options[user_input]()

)来调用该函数

对不起,我不完全理解你在10次之后的提示你做了什么,因为你应该在他们陷入困境时提示他们纠正他们的输入,但我相信用这种方法做你做的事情想做的事情会是这样的:

#Count to see how many times they mess up
count = 0
#Options dictionary
options = {     1 : option_1,
                2 : option_2,
                3 : option_3
}

#I am using Python 3 BTW
print("Enter 1, 2 or 3 ")
#And I am assuming they will give good input
user_input = int(raw_input("1, 2, or 3"))
#This more closly mimics what you are doing but like I said I would avoid this so you don't have to hard code how many options you have
#while((user_input is not 1) or (user_input is not 2) or (user_input is not 3))
#try this
#It makes sure you are not out of bounds for an any number of elements
while(user_input < 0 and user_input > len(options)):
    #if you are ask for new input
    user_input = int(raw_input("1, 2, or 3"))
    #increment count
    count += 1
    #check if they messed up 10 times 
    if(count == 10):
        print("Enter 1, 2, or 3")
        #reset count so it will tell them on the next 10th time
        count = 0

#to break the while loop above you must have valid input so call the function 
options[user_input]()

#And of course you need to define you functions to do the different options
def option_1():
    do option_1 stuff

def option_2():
    do option_2 stuff

def option_3():
    do option_3 stuff

虽然这与你所拥有的非常不同,但是以这种方式添加新选项要容易得多,因为你只需要添加一个新函数并将选项添加到字典中而你不必担心测试对于您拥有的每个选项。

TL; DR:Python词典是输入的方式,不测试每个案例

答案 3 :(得分:0)

或者您可以使用此示例代码:

choices_menu = '''
Please select input menu :
Enter 1> Show latest 100 data
Enter 2> Show latest 1000 data
Enter 3> Show all data'''

choice = 0
while(choice>3 or choice<1):
    print choices_menu
    choice = raw_input("Your choice (Input number 1 to 3) ? ")
    try:
        choice = int(choice)
        if choice >= 1 and choice <= 3:
            break
    except:
        continue

print choice

结果:

Please select input menu :
Enter 1> Show latest 100 data
Enter 2> Show latest 1000 data
Enter 3> Show all data
Your choice (Input number 1 to 3) ? -1

Please select input menu :
Enter 1> Show latest 100 data
Enter 2> Show latest 1000 data
Enter 3> Show all data
Your choice (Input number 1 to 3) ? 0

Please select input menu :
Enter 1> Show latest 100 data
Enter 2> Show latest 1000 data
Enter 3> Show all data
Your choice (Input number 1 to 3) ? 5

Please select input menu :
Enter 1> Show latest 100 data
Enter 2> Show latest 1000 data
Enter 3> Show all data
Your choice (Input number 1 to 3) ? 4

Please select input menu :
Enter 1> Show latest 100 data
Enter 2> Show latest 1000 data
Enter 3> Show all data
Your choice (Input number 1 to 3) ? 3
3

Process finished with exit code 0
相关问题