简体   繁体   中英

The random.randint import doesn't seem to be working correctly

This is my first question on Stack and I am really hoping someone else has had this same problem and can help me.

I have written the below code using Python 3.7.6 that randomly picks a card suit and a corresponding value based on a dict I have built. This code was working absolutely fine yesterday and the day before, however, today the random_number variable only seems to select 0 for the value_2 variable. Any help would be greatly appreciated. Thanks:

import random
keys = list(deck.keys())

def random_card_generator():
    random_number = random.randint(0,8)
    random_word = random.choice(keys)

    if random_word.find('Ace') != -1:
        choose = 0
        print(random_word)
        while choose != 1 or choose != 11:
            choose = int(input('Would you like 1 or 11?: '))
            if choose == 1 or choose == 11:
                return choose, random_word
                break
            else:
                print('That is not a valid number. Please choose either 1 or 11')
                choose
    elif random_word.find('King') or random_word.find('Jack') or random_word.find('Queen') != -1:
        value = deck[random_word][0], random_word
        return value
    else:
        value_1 = deck[random_word][random_number], random_word
        return value_1

It's hard to be certain without seeing how your deck is structured, but I think the error is in this line:

elif random_word.find('King') or random_word.find('Jack') or random_word.find('Queen') != -1:

This needs to be

elif random_word.find('King') != -1 or random_word.find('Jack') != -1 or random_word.find('Queen') != -1:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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