Allow Python App To Continue Running Until User Ends

后端 未结 1 551
天涯浪人
天涯浪人 2021-01-24 12:55

I\'m pretty new to python and I wrote a simple blackjack card counting app. The app runs and after the user input it ends, I want it to continue asking for the user input and pr

相关标签:
1条回答
  • 2021-01-24 13:47

    To make your program run until the user stops providing input, you will probably need to put your code in a loop. A while loop is usually the best when you don't know ahead of time how long you'll need to loop for.

    count = 0
    while True: # runs forever
        var = int(raw_input("Enter the value of the card: "))
    
        # if/elif block goes here
    

    Once you do this though, you'll find that there's a logic error in your code. Currently you're printing count + 1 or similar in one of your if/elif blocks each time. However, this doesn't modify the value of count, it just prints the new value directly. What I think you'll want to do is change your code to directly modify count in the if/else blocks, then write it out in a separate step. You can also combine several of your conditions using < or >, like this:

        if var < 7:
            count += 1
        elif var > 9:
            count -= 1
    
        print count
    

    You could optionally add some extra error checking to make sure the entered value is appropriate (e.g. between 1 and 11), but I've left that off here for clarity.

    0 讨论(0)
提交回复
热议问题