Python 用户输入和while循环

Python 用户输入和while循环,python,loops,input,while-loop,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 (

我正在尝试编写一个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,则程序的行为与我希望的一样

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

代码:


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

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

while(choice>3 or choice<1):
当然,这意味着您必须将代码放在函数中才能实现语法如下所示

def function():
    do stuff
请注意,在字典中,函数是如何存储的,而不使用。之所以这样做,是因为没有括号,它是对函数本身的引用。如果您进行打印,您将看到它为您提供内存地址,使用括号,函数将执行。然后,您只需通过向词汇表查找添加括号来调用函数,如下面的选项[user\u 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字典是输入的方式,不要对每种情况进行测试,代码要尽可能与您的代码相似:

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

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

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

这是预期的行为。如果选项为ǸOT 1,2,3,则跳过该块,否则执行该块!是的,我明白,我知道我可以在我的第一个while循环的基础上做另一个while循环,即while choice==1或choice==2或choice==3,count<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
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
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