Kirmaan Aboobaker
Kirmaan Aboobaker

Reputation: 11

Allow Python App To Continue Running Until User Ends

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 printing the count out. I don't want it to restart, I want it to continue. Ive included the source code. Thanks guys.

count = 0

var = int(raw_input("Enter the value of the card: "))

if var == 1 :
      print count + 1
elif var == 2 :
      print count + 1
elif var == 3 :
      print count + 1
elif var == 4 :
      print count + 1
elif var == 5 :
      print count + 1
elif var == 6 :
      print count + 1
elif var == 7 :
      print count + 0
elif var == 8 :
      print count + 0 
elif var == 9 :
      print count + 0 
elif var == 10 :
      print count - 1 
elif var == 11 :
      print count - 1

Upvotes: 1

Views: 382

Answers (1)

Blckknght
Blckknght

Reputation: 104712

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.

Upvotes: 1

Related Questions