Home > Enterprise >  How to prevent entering two of the same numbers in 2 or more inputs
How to prevent entering two of the same numbers in 2 or more inputs

Time:09-23

I was wondering how i can stop a user from inputing the same number twice in this code. So i ask "Input the first number", Input the second number", "Input the third number". If it is possible to prevent him saying 2, 2 and 5 or something, it has to be different numbers, all 3. I'm trying to find the middle number btw. I already did that.

def middle(num1, num2, num3) :
return min(max(num1,num2),max(num2,num3),max(num1,num3))

num1 = input("Sláðu inn fyrstu tölu: ")
num2 = input("Sláðu inn seinni tölu: ")
num3 = input("Sláðu inn þriðju tölu: ")
print("Miðju talan er: ")
print(middle(num1,num2,num3))

CodePudding user response:

I mean:

if num1 == num2 or num1 == num3 or num2 == num3:

CodePudding user response:

Try creating an endless loop that only breaks, if the number is different from the first numbers.

num1 = input("Sláðu inn fyrstu tölu: ")
while True:
    num2 = input("Sláðu inn seinni tölu: ")
    if num2 == num1:
        print("This number has already been choosen")
        continue
    else:
        break
while True:
    num3 = input("Sláðu inn seinni tölu: ")
    if num3 == num1 or num3 == num2:
        print("This number has already been choosen")
        continue
    else:
        break

CodePudding user response:

Got it. Shoutout to Recoded for helping me so fast. So you need to put this " if num1 == num2 or num1 == num3 or num2 == num3: print("Bannað að slá inn sömu tölu.") else: print("Miðju talan er: ") print(middle(num1,num2,num3))" into the code :)

def middle(num1, num2, num3) :
    return min(max(num1,num2),max(num2,num3),max(num1,num3))
num1 = input("Sláðu inn fyrstu tölu: ")
num2 = input("Sláðu inn seinni tölu: ")
num3 = input("Sláðu inn þriðju tölu: ")
if num1 == num2 or num1 == num3 or num2 == num3:
    print("Bannað að slá inn sömu tölu.")
else:
    print("Miðju talan er: ")
    print(middle(num1,num2,num3))       
  • Related