python - While Loop Complications -
i'm writing python program based on accordion style of solitaire. i've written above code , have been playing around last couple hours can't seem make loop run correctly. whatever reason either run through loop once , not ask input again, or run through once, ask input, , no matter input crashes. ideas i'm doing wrong here?
here code:
import random print("command options:") print("1c - play card c onto pile 1 position back, ignored if invalid") print("3c - play card c onto pile 3 positions back, ignored if invalid") print("c - count of undealt cards") print("d - deal next card") print("h - print screen") print("r - resign game (quit early)") print("x - exit program") cards=['ac','2c','3c','4c','5c','6c','7c','8c','9c','tc','jc','qc','kc','ad','2d','3d','4d','5d','6d','7d','8d','9d','td','jd','qd','kd','ah','2h','3h','4h','5h','6h','7h','8h','9h','th','jh','qh','kh','as','2s','3s','4s','5s','6s','7s','8s','9s','ts','js','qs','ks'] random.shuffle(cards,random.random) playable=[] done=false while not done: move=input("enter move: ") move_upper=move.upper() if move_upper == 'd': playable.append(cards.pop()) print(playable) if move_upper == 'x' or 'r': done=true if move_upper == 'h': print("command options:") print("1c - play card c onto pile 1 position back, ignored if invalid") print("3c - play card c onto pile 3 positions back, ignored if invalid") print("c - count of undealt cards") print("d - deal next card") print("h - print screen") print("r - resign game (quit early)") print("x - exit program") if move_upper == 'c': k=0 item in cards: k+=1 print(k,'cards left')
you misunderstanding how or
works. use:
if move_upper in ('x', 'r'):
instead.
the expression move_upper == 'x' or 'r'
interpreted (move_upper == 'x') or 'r'
instead, , non-empty string always considered true
. thus, in essence testing (move_upper == 'x') or true
, , doesn't matter anymore move_upper
is, really.
you don't need use done
flag variable here; use break
end loop instead:
while true: # ... if move_upper in ('x', 'r'): break
Comments
Post a Comment