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

时间:2017-05-15 19:18:58

标签: 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

1 个答案:

答案 0 :(得分:0)

好吧,让我们让你的代码变得更加pythonic ......

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特性,可以帮助你获得一些知识。