-1

I'm new to here. This is my first post, I'm sorry if I get something wrong, but here is my question; I'm making a game in py3 and I'm unsure on how to get it to exit the loop but have 2 different outcomes. I believe it will be harder to explain than to just show you the code, so here it is:

a = 0
start = raw_input("Type c to continue previous game or press n to start a new one!")
while start != (start == "c" and start == "n"):
    print"Try again!"
    start = raw_input("c or n")
if start =="c":
    print "continuing game!"
    balance-=balance
    a-=a
    a+=2
elif start =="n":
    print "Starting new game!"
    a -=a
    a +=1

It just continues the loop even when c or n is entered. Any help is appreciated!

Ethan L
  • 23
  • 4
  • I'd make it `while True`, and then put the logic in question in an `else` clause; that'll always be activated when you have no specific handler for the key in question, so you don't need to keep updating the list when you add other keystrokes elsewhere. – Charles Duffy Oct 16 '15 at 21:52
  • 1
    As an aside, why are you doing `a -=a ; a +=1` when this is the same as `a=1`? – RobertB Oct 16 '15 at 21:54
  • 1
    That said, you can also write `while not start in ('c', 'n')`; there are more operators than just `!=`. – Charles Duffy Oct 16 '15 at 21:54
  • @CharlesDuffy as strings are already iterable, could you do `while not start in 'cn':` ? – Jonah Graham Oct 16 '15 at 21:58
  • Surely could, but I'm not sure if it's as readable. – Charles Duffy Oct 16 '15 at 21:58
  • How is this a duplicate of http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response? They're not even remotely close to being about the same problem. – rolling stone Oct 16 '15 at 22:31

1 Answers1

3

Try:

while (start != "c") and (start != "n"):

The error is happening you're actually checking that start is not equal to start being equal to both 'c' and 'n', which will never happen.

Update You can also do what @CharlesDuffy recommends above, which is cleaner:

while not start in ('c', 'n'):
rolling stone
  • 12,668
  • 9
  • 45
  • 63
  • 1
    `while start not in ('c', 'n')` is even better. or maybe `while start not in 'nc'` which is shorter but not so clear. – imposeren Oct 16 '15 at 21:58
  • @rolling-stone I get an error using code: a = 0 start = raw_input("Type c to continue previous game or press n to start a new one!") while not start in ('c', 'n'); print"Try again!" start = raw_input("c or n") if start =="c": print "continuing game!" balance-=balance a-=a a+=2 elif start =="n": print "Starting new game!" a -=a a +=1 and the error is an invalid syntax on the ; – Ethan L Oct 16 '15 at 22:03
  • `while start not in 'nc'` is probably the canonical version. – TigerhawkT3 Oct 16 '15 at 22:04
  • Thanks! the while (start != "c") and (start != "n"): works perfectly! – Ethan L Oct 16 '15 at 22:08
  • @EthanL looks like you may be using a semicolon `;` instead of a colon `:`. Just updated that in my answer. – rolling stone Oct 16 '15 at 22:08