Python卡猜谜游戏

时间:2013-11-22 20:57:17

标签: python

我有一个任务,程序随机选择一个套装和一个卡值(1-13)(11,12,13应该是杰克皇后和国王),然后用户应该猜测它。如果猜测卡太高或太低,该程序也应该在猜测卡值时作出响应。我得到它的工作,除非程序猜测11或12或13或1(ace)。我无法将值分配给面部卡片的单词,以便用户可以猜出卡片的面部,程序会将其检测为值。顺便说一句,这是python。非常感谢任何有帮助的人。如果有更简单的方法来解决这个问题,我也很乐意学习。(这是我更新的代码,现在我收到错误)说的是Guess_number.lower()。strip()) AttributeError:'NoneType'对象没有属性'lower'

import random

SUITS = ("D", "H", "C", "S")
suit = random.choice(SUITS)

card_names = random.randint( 1,13)

card_names = {'ace': 1, 'jack': 11, 'queen': 12, 'king': 13}
for i in range(2, 11):
    card_names[str(i)] = i
guessCount = 0


Guess_Suit = input("Start by guessing the Suit:")

guessCount = guessCount + 1
while guessCount < 9:
    Guess_Suit = Guess_Suit.upper()
    guessCount = guessCount + 1
    if Guess_Suit == suit:
        print("Correct!")
        break
    elif Guess_Suit != suit:
        print('Wrong')
        Guess_Suit = input("Guess the Suit again: ")

Guess_number = input("Now Guess the card number or name of the face card:")
Guess_number = card_names.get(Guess_number.lower().strip())
guessCount = guessCount + 1
while guessCount < 9:
    Guess_number = card_names.get(Guess_number.lower().strip())
    if Guess_number > number:
        print("The card you picked is too high")
        Guess_number = input("Pick another one")
        guessCount = guessCount + 1
        Guess_number = card_names.get(Guess_number.lower().strip()) 


    if Guess_number < number:
        print("The card you picked is too low")
        Guess_number = input("Pick another one")
        guessCount = guessCount + 1
        Guess_number = card_names.get(Guess_number.lower().strip())
    elif Guess_number == number:
        print("THATS IT YOU WON")
        break

1 个答案:

答案 0 :(得分:1)

创建一个从卡名称映射到关联号码的字典,例如:

card_names = {'ace': 1, 'jack': 11, 'queen': 12, 'king': 13}
for i in range(2, 11):
    card_names[str(i)] = i

然后使用以下内容替换所有Guess_number = int(Guess_number)行:

Guess_number = card_names.get(Guess_number.lower().strip())

如果此后Guess_numberNone,则表示他们提供了无效值,因为找不到卡名或提供的数字太低/太高。

编辑:说明一定不够清楚,这里是我建议的更改的完整代码:

import random

SUITS = ("D", "H", "C", "S")
suit = random.choice(SUITS)

number = random.randint( 1,13)

card_names = {'ace': 1, 'jack': 11, 'queen': 12, 'king': 13}
for i in range(2, 11):
    card_names[str(i)] = i
guessCount = 0

Guess_Suit = input("Start by guessing the Suit: ")

guessCount = guessCount + 1
while guessCount < 9:
    Guess_Suit = Guess_Suit.upper()
    guessCount = guessCount + 1
    if Guess_Suit == suit:
        print("Correct!")
        break
    elif Guess_Suit != suit:
        print('Wrong')
        Guess_Suit = input("Guess the Suit again: ")

Guess_number = input("Now Guess the card number or name of the face card:")
Guess_number = card_names.get(Guess_number.lower().strip())
guessCount = guessCount + 1
while guessCount < 9:
    if Guess_number is None:
        print("The card you picked is invalid")
        Guess_number = input("Pick another one: ")
        Guess_number = card_names.get(Guess_number.lower().strip())
        guessCount = guessCount + 1
    elif Guess_number == number:
        print("THATS IT YOU WON")
        break
    else:
        print('Wrong')
        Guess_number = input("Pick another one: ")
        Guess_number = card_names.get(Guess_number.lower().strip())
        guessCount = guessCount + 1