aaoo
aaoo

Reputation: 33

How to make a variable equal to multiple strings?

import random
verify0 = random.randint(0, 9)
verify1 = random.randint(0, 9)
verify2 = random.randint(0, 9)
print(verify0, end = '')
print(verify1, end = '')
print(verify2)
verans0 = int(input('Type the above number here: > '))
verans2 = {verify1, verify0, verify2}
if verify1 in verans2:
    print('Authenticating...')
else:
    print('Incorrect!')
    while verans0 != verans2:
        verans1 = input('Type the above number here: > ')
        if verans1 == verify0:
            print('Authenticating...')
        else:
            print('Incorrect!')

I am wondering how I can print multiple numbers, and have someone type in all of the numbers in order to be 'authenticated.'

Upvotes: 0

Views: 950

Answers (2)

CryptoFool
CryptoFool

Reputation: 23119

So I assume you want to show 3 numbers and have the user enter those three numbers to be authenticated. I won't write your whole program for you, but here are some tips that hopefully, together, will lead you to an answer.

You have a problem with your code in putting the 3 numbers into a set. If the three calls to randint happen to produce matching numbers, your set will have fewer than three numbers in it because sets can't contain duplicates. So I'd use a list, assuming that you want the user to always enter 3 numbers even if two (or three) of them happen to be the same.

To have the user enter the three numbers that result in a list of ints, you could use this statement:

verans0 = [int(i) for i in re.split(r'[^\d]+', input('Type the above numbers here: '))]

This will pick integers separated by any non-digit characters into an array of ints. There is one case that will break this, and that is entering a string with no digits in it. That will lead to trying to parse a non-integer string. I'll leave that to you to fix through a preflight of the input. You also might want to play with what delimiters you allow, like just whitespace, or whitespace and commas, or whatever.

To verify the input, I'd loop over the input array and for each number input:

  1. confirm that it's in the verans2 list. If it isn't, the authentication failed
  2. if the number is in verans2, remove it (just one copy of it if there are duplicates) from that list

If you make it through the list without a mismatch, check to see if the verans2 list is empty. If it isn't, then the user failed to enter all the digits, and the authentication failed. If it is empty, then you have a successful authentication.

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51683

You can simply create the correct number from your random digits (or use n = random.randint(100,999):

import random
verify0 = random.randint(0, 9)
verify1 = random.randint(0, 9)
verify2 = random.randint(0, 9)
number = verify0*100+verify1*10+verify  # or: n = random.randint(100,999)
print(number)

while True:
    try:
        verans0 = int(input('Type the above number here: > '))
    except ValueError:
        continue
    else:
        if verans0 == number:
            print('Authenticating...')
            break  # exit the while True loop
        else:
            print('Incorrect!')

print("done")

Upvotes: 1

Related Questions