简体   繁体   English

尝试除非两个输入相同

[英]try except if two inputs are the same

I currently have a while loop that looks like this:我目前有一个 while 循环,看起来像这样:

while True:
    try:
        entry1 = input("input the first number")
        entry2 = input("enter the second number")
        break
    except ValueError:
        print("number must be valid re-enter")

I want to add code that will reprompt the user to enter two different numbers if the user decides to enter two numbers that are the same.我想添加代码,如果用户决定输入两个相同的数字,将重新提示用户输入两个不同的数字。 I tried using the following:我尝试使用以下内容:

while True:
    try:
        entry1 = input("input the first number")
        entry2 = input("enter the second number")
        break
    except ValueError:
        print("number must be valid re-enter")
    try: 
        if entry1 == entry2:
            raise ValueError
        else:
            break
    except ValueError as err:
        print("The two numbers cant be the same. re-enter two unique numbers!"

Unfortunately this is not working.不幸的是,这是行不通的。 Does anyone know what is going wrong with my code?有谁知道我的代码出了什么问题?

you can use assert method to make sure the 2 inputs are different and catch with AssertionError if they don't.您可以使用assert方法来确保 2 个输入不同,如果不同,则使用AssertionError捕获。

while True:
    try:
        entry1 = int(input("input the first number"))
        entry2 = int(input("enter the second number"))
        assert entry1 != entry2
        break
    except ValueError:
        print("number must be valid re-enter")
    except AssertionError:
        print("The two numbers cant be the same. re-enter two unique numbers!")

For what you are describing you don't need all these exceptions.对于您所描述的内容,您不需要所有这些例外。 You could for example just use the comparison as condition for a while loop:例如,您可以只使用比较作为 while 循环的条件:

entry1 = entry2 = 0
while entry1 == entry2:
    entry1 = input("input the first number")
    entry2 = input("enter the second number")
    if entry1 == entry2:
        print("The two numbers can't be the same. re-enter two unique numbers!")

Edit: Of course it makes sense to cast the input to int and catch the error if that doesn't work.编辑:当然,将输入转换为 int 并在不起作用时捕获错误是有意义的。 That could be like this:那可能是这样的:

entry1 = entry2 = 0
while entry1 == entry2:
    try:
        entry1 = int(input("input the first number"))
        entry2 = int(input("enter the second number"))
        if entry1 == entry2:
            print("The two numbers can't be the same. re-enter two unique numbers!")
    except ValueError:
        print("Please enter valid numbers")

break are called incorrectly. break被错误地调用。 Too many try-except block. try-except块太多。

 while True:
    try:
        entry1 = input("input the first number: ")
        entry2 = input("enter the second number: ")
    except ValueError:
        print("number must be valid re-enter.")

    if entry1 == entry2:
            print("The two numbers cant be the same. re-enter two "
                  "uniquen numbers!")
    else :
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM