0

Apologies for basic nature of this query - this is almost my first Python script.

I want the user to enter a two character string corresponding to a playing card (eg 2c, 3s) and for the program to check two things: first, whether it's in the list of valid cards, and second, whether that card has already been entered. If either condition is failed, I want the user to be prompted to re-enter the card. What I have written does each of those things, but it doesn't do them simultaneously - eg, if the card is invalid, the user is prompted, but if the re-entered card is already on the list, no prompt occurs. I can't see how to get both things tested at once.

Anyway, here is what I have:

posscards=["ac",
           "2c",
           "3c",
           "4c",
           "5c" #and so on] 


for x in range(0, decksize):
        answer=raw_input("next card? ")
        while answer not in posscards:
            answer = raw_input("not a possible card - try again: ")
        while answer in deck1:
            answer = raw_input ("you've already had that one - try again: ")
        deck1.append(answer)
1
  • As a side note, you might want to write some code to generate the postcards list instead of writing it out manually. Then you only need to write 13+4 values instead of 13*4, and you can write those 13 values as just a 13-character string. For example, posscards = [value+suit for suit in 'cshd' for value in 'a23456789tjqk'] Even if you don't understand that nested list comprehension (which you probably don't), you should be able to write it as a 4-line for statement. Commented Nov 14, 2013 at 22:40

2 Answers 2

3

Instead of two while loops, you can use a single while loop with two if conditions. For example:

prompt = "next card? "
while True:
    answer = raw_input(prompt)
    if answer not in posscards:
        prompt = "not a possible card - try again: "
    elif answer in deck1:
        prompt = "you've already had that one - try again: "
    else:
        deck1.append(answer)
        break

It might be a little more readable if you factored it out into a function:

def get_card():
    while True:
        answer = raw_input(prompt)
        if answer not in posscards:
            prompt = "not a possible card - try again: "
        elif answer in deck1:
            prompt = "you've already had that one - try again: "
        else:
            return answer

Now, every time you call get_card, it's guaranteed to (eventually) return an answer. So, your main loop can look like this:

deck1 = []
for x in range(decksize):
    answer = get_card()
    deck1.append(answer)

And you can turn that into a one-liner:

deck1 = [get_card() for x in range(deck size)]
Sign up to request clarification or add additional context in comments.

Comments

0

by the way I don' see deck1 anywhere. make sure you declare before you try to append things to it.

deck1 = []

while answer not in posscards and answer in deck1:
     answer = raw_input("not a possible card or card already entered- try again: ")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.