简体   繁体   English

如何检查用户输入是否在字典中

[英]how to check if user input is in a dictionary

how do I check if a value that the user has given is in a dictionary of randomly generated numbers and that the key and value match?如何检查用户给出的值是否在随机生成的数字字典中并且键和值匹配?

card_numbers = {}

def create_card():
    first_half = 400000
    second_half = random_digits_gen(10)
    card_num = int(str(first_half) + str(second_half))
    pin_num = random_digits_gen(4)
    store_nums(card_num, pin_num)
    print('Your card has been created\n'
          'Your card number:\n'
          f'{card_num}\n'
          'Your card PIN:\n'
          f'{pin_num}')


def store_nums(number, pin):
    card_numbers[number] = pin


def check_nums():
    card_num = input('Enter your card number:\n')
    card_pin = input('Enter your pin:\n')
    if card_num in card_numbers.keys() and card_pin in card_numbers.values():
        print('Success!')
    else:
        print('nope')

create_card()
check_nums()

What is the right way to check for this?什么是检查这个的正确方法?

You have to first check that the card_num input by the user exists in the card_numbers .您必须首先检查用户输入的card_num是否存在于card_numbers中。 In case of existence, check that the values does match.如果存在,请检查值是否匹配。 Your function should look like this:您的 function 应如下所示:

def check_nums():
    card_num = int(input('Enter your card number:\n'))
    card_pin = int(input('Enter your pin:\n'))

    stored_card_pin = card_numbers.get(card_num)  # does return None if key is not found
    if stored_card_pin is not None:
        print('Success!' if stored_card_pin == card_pin else 'nope')

Looks like you are checking if the card number and the card pin are part of the dictionary.看起来您正在检查卡号和卡针是否是字典的一部分。 Also as @goalie1998 mentioned, you need to convert the input into integers.同样正如@goalie1998 提到的,您需要将输入转换为整数。

The way your code is currently setup, if you have the following examples:如果您有以下示例,则当前设置代码的方式:

card_numbers{100:20,200:40}

and your inputs are 100 and 40 , your code will be True.并且您的输入是10040 ,您的代码将为 True。

Let's examine it.让我们检查一下。

The first part of the if statement if 语句的第一部分

if card_num in card_numbers.keys() will be True as 100 is a key. if card_num in card_numbers.keys()为 True,因为100是一个键。

It will then check the second part:然后它将检查第二部分:

and card_pin in card_numbers.values()

This will also be True as 40 is one of the values in the card_numbers dictionary.这也将是 True,因为40是 card_numbers 字典中的值之一。 However, 40 is not part of 100 .但是, 40不是100的一部分。

The correct way to check this will be as shown below.正确的检查方法如下所示。

if card_num in card_numbers and card_pin in card_numbers[card_num]:

The way python checks is from left to right. python 检查的方式是从左到右。 So it goes through the first if statement ( if card_num in card_numbers ).所以它通过第一个 if 语句( if card_num in card_numbers )。 If this is True , then it checks if card_pin is in card_numbers[card_num] .如果这是True ,那么它会检查card_pin是否在card_numbers[card_num]中。

If card_num is not in card_numbers , it exits the if statement.如果card_num不在card_numbers中,则退出 if 语句。 So it never goes to check if card_pin is in card_numbers .所以它永远不会检查card_pin是否在card_numbers中。

By keeping the correct order of the if statement, you can avoid error handling situations.通过保持 if 语句的正确顺序,您可以避免错误处理情况。

The updated code will be as follows:更新后的代码如下:

def check_nums():
    while True:
        try:
            card_num = int(input('Enter your card number:\n'))
            card_pin = int(input('Enter your pin:\n'))
            break
        except:
            print('input not a number. retry')
    if card_num in card_numbers and card_pin in card_numbers[card_num]:
        print('Success!')
    else:
        print('nope')

You need to first check that card_num exists in the dictionary and then compare the card_pin against that card number:您需要首先检查字典中是否存在card_num ,然后将card_pin与该卡号进行比较:

def check_nums():
    card_num = input('Enter your card number:\n')
    card_pin = input('Enter your pin:\n')
    if card_num in card_numbers and card_pin == card_numbers[card_num]:
        print('Success!')
    else:
        print('nope')

1/ you have to chose how you store your card numbers and PIN. 1/ 您必须选择存储卡号和 PIN 的方式。 As leading zero can be important I recommend to store them as strings.由于前导零可能很重要,我建议将它们存储为字符串。 Otherwise your pin number or card number first and second half could not stard with 0, without being shortened.否则您的密码或卡号前半部分和后半部分不能以 0 开头,而不会被缩短。

2/ second your card_numbers variable should be made global in each function, so the same variable is used in the whole piece of code. 2 / 秒你的 card_numbers 变量应该在每个 function 中成为全局变量,因此在整段代码中使用相同的变量。

3/ you have to change the pin comparison part like this: card_numbers[card_num] == card_pin 3/您必须像这样更改引脚比较部分:card_numbers[card_num] == card_pin

Here is the whole code, I just replaced random_digits_gen calls by hardcoded numbers to make things easier for testing.这是整个代码,我只是用硬编码的数字替换了 random_digits_gen 调用,以使测试更容易。 Here the card number is 4020 and pin is 1111.这里卡号是 4020,密码是 1111。

card_numbers = {}

def create_card():
    first_half = "40"
    second_half = "20" ## random_digits_gen(10)
    card_num = str(first_half) + str(second_half)
    pin_num = "1111" ## random_digits_gen(4)
    store_nums(card_num, pin_num)
    print('Your card has been created\n'
          'Your card number:\n'
          f'{card_num}\n'
          'Your card PIN:\n'
          f'{pin_num}')


def store_nums(number, pin):
    global card_numbers
    card_numbers[number] = pin


def check_nums():
    global card_numbers
    card_num = input('Enter your card number:\n')
    card_pin = input('Enter your pin:\n')
    if card_num in card_numbers.keys() and card_numbers[card_num] == card_pin:
        print('Success!')
    else:
        print('nope')

create_card()
check_nums()

Output is: Output 是:

Enter your card number:
4020

Enter your pin:
1111
Success!

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

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