Python中更高或更低的纸牌游戏



我需要编写一些代码来显示从1到12的随机单个数字/牌,然后玩家猜测下一张牌是高于还是低于第一张牌/号码。(打你的牌正确英国游戏(如果他们猜对了,他们就会再试一次,如果他们猜错了(他们说更高或更低,恰恰相反(,他们输了,游戏结束。如果他们连续猜对了四个,他们就赢了。

我对如何做到这一点有一个非常粗略的想法。

你能伸出援手吗?

import random
guessesTaken = 0 
print('Hello! I am going to show you a card, guess whether the next card is higher or lower, get four in a row to win!')

number = random.randint(1, 12)
number1 = random.randint(1, 12)
number2 = random.randint(1, 12)
number3 = random.randint(1, 12)
number4 = random.randint(1, 12)
#five variables for five cards, all random cards between 1 and 12
print('Well I am thinking of a card between 1 and 12, the first number is:')
print (number) #shows them first card
while guessesTaken < 5: #limit number of guesses to make game less than 4 to win?
    print('Take a guess, is the next card higher or lower? Please enter a number from 1 to 12.') 
    guess = input()
    guess = str(guess) # limit input to "h" or "l"?
    guessesTaken = guessesTaken + 1 #increment guesses taken
    if guess = h and guess !> number1:
    print ("you lose")
        break
    if guess =l and guess !< number1:
        print('you lose')
    if guess = h and guess !< number1:
    print ("well done")
        #ask about next card
    if guess =l and guess !> number1:
        print('well done')
        #ask about next card

    if guess == number1:
        print ('you lose it was neither higher nor lower')
        break
    #basically I know the middle comparison for values higher or lower for the four cards can be done with a loop, just not sure how, very new to this. 

第二版(工作(

import random
guessesTaken = 0 
print('Hello! I am going to show you a card, guess whether the next card is higher or lower, get four in a row to win!')
number = random.randint(1, 12)
#five variables for five cards, all random cards between 1 and 12
print('Well I am thinking of a card between 1 and 12, the first number is:')
print (number) #shows them first card
guess = input('Take a guess, is the next card higher or lower? Please enter either "h" or "l".')
while guessesTaken <= 4: #limit number of guesses to make game less than 4 to win?
    if guessesTaken == 4:
        print("You win")
        break
    else:
        nextnumber = random.randint(1,12)
        print(nextnumber)
        if guess == "h" and nextnumber <= number:
            print ("you lose")
            break
        elif guess == "l" and nextnumber >= number:
            print ("you lose")
            break
        else:
            guess = input("the card was" + str(nextnumber) + "is the next card higher or lower?")
        guessesTaken += 1 #increment guesses taken
        number = nextnumber

好吧,让我们让你的代码更python化一点......

import random
DECK_SIZE = 5
# See that it generates 5 unique random number from 1 to 12 
# (not including 13) thus simulating a deck of cards, 
# where cards cannot repeat. We add a None in the end to
# indicate end of deck
deck = random.sample(range(1, 13), DECK_SIZE) + [None]
# use of the zip method to create pairs of cards to compare 
# (the final card will be a None, so we can see when cards ended)
for card,next_card in zip(deck[:-1],deck[1:]):
    if next_card == None: # End of deck, user won the game
        print("You won")
        break
    # we calculate the expected correct guess 
    # based on the pair of cards
    correct_guess = "h" if next_card > card else "l"
    # We get the user input
    guess = input("card is {card} is next h or l? ".format(card=card))
    # We verify if user input is the expected correct guess
    if guess != correct_guess:
        print("You lost, card was {next_card}".format(next_card=next_card))
        break

我希望您可以通过学习此代码并进行更改以满足您的需求来提高您的 Python 技能。它使用了一些python特性,可以帮助您获得一些知识。

最新更新