-4

I've been having issues with my programming homework, I'm trying to add a while statement, so if the user types in a response that the code doesnt understand, it will try again, when I compile the code it gets stuck on line 4 and 5, please help!

print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = 'none'
while path = none:
    print('You walk down a corridor, do you go left or right?')
    response = input()
    if 'left' in response:
        print('You take the left turn.')
        path = 'left'
    elif 'right' in response:
        print('You take the right turn.')
        path = 'right'
    else:
        print('Sorry, I didnt understand')
        path = 'none'
WGS
  • 13,969
  • 4
  • 48
  • 51
  • 1
    Your code has numerous errors. Please paste the complete error message of the problem you're asking about. It would also help to explain how you've tried to solve it, as people here are usually not very eager to do your homework for you unless you show that you have made an effort to solve the problem yourself. – BrenBarn Mar 28 '14 at 22:39
  • 1
    I've got no idea, I understand, you guys can't do it for me. Here's the main bit I'm stuck at: – user3463732 Mar 28 '14 at 22:42
  • Edited back the code, as I think it's integral you keep the entire code on your question. Formatted it properly with indentation. If this is not your indentation originally, feel free to rollback the changes. – WGS Mar 28 '14 at 22:54

6 Answers6

1
while path = none:

What you're doing here is assigning none to path, not checking for the string 'none' in path.

It'd need to be changed to check the variable instead of setting it:

 while path is 'none':
Leigh
  • 12,038
  • 4
  • 28
  • 36
1
print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = None
while path is None:
    print('You walk down a corridor, do you go left or right?')
    response = input()
    if 'left' in response:
        print('You take the left turn.')
        path = 'left'
    elif 'right' in response:
        print('You take the right turn.')
        path = 'right'
    else:
        print('Sorry, I didnt understand')
Jack
  • 20,735
  • 11
  • 48
  • 48
1

The block of code you wish to loop over with your while statement must be indented, i.e.

print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = 'none'
while path = none:
   print('You walk down a corridor, do you go left or right?')
   response = input()
   if 'left' in response:
      print('You take the left turn.')
      path = 'left'
   elif 'right' in response:
      print('You take the right turn.')
      path = 'right'
   else:
      print('Sorry, I didnt understand')
      path = 'none'

Secondly, you're conflating assignment (=) with equality (==) in your while loop condition. It should be:

while path == 'none':
   ...

Also, I think your use if in in the if statements won't work as you intend. The in infix operator checks if the left operand is an element of the right operand collection. You could try the following str method:

if response.startswith('....')

or compare the left substring of the response as follows

if response[:4] == 'left'
mtadd
  • 2,495
  • 15
  • 18
0

My knowledge of python syntax is quite limited but I'm fairly sure you would need to add a break so that your while Loop finishes.

Emulate a do-while loop in Python?

this seems similar and might help! :-)

edit: I think the above answer nailed it just as I noticed. :D

Community
  • 1
  • 1
li x
  • 3,953
  • 2
  • 31
  • 51
0

I can see a few problems and possible causes for concern. For instance, you should use raw_input() instead of input as it is safer (where input() calls eval(raw_input()) in a sense, which evaluates the entered text, so that if I say my name is 1+2 my name with input() is 3, but with raw_input() is "1+2") (Note that this is provided you are using Python 2.x, I believe it is different for 3.X)

Also, your while loop doesn't appear to have any indentation, as a rule any colon should be followed by a new level of indentation.

Finally your while line has a couple more problems, namely that you are using a single = (instead of ==) to compare two values, and that none is not surrounded by '' (there is no variable called none!) alternatively, you could have wanted to say None, in which case the declaration path = none should be None (and the same in the while loop)

J_mie6
  • 720
  • 1
  • 9
  • 25
  • Using `input` is not inherently wrong: remember that Python 3.x uses it, whereas Python 2.x uses `raw_input`. From OP's code, it's pretty deterministic he's using Python 3.x given that his `print` is not a function but a statement. It will be good to note this in your answer as well. ;) – WGS Mar 28 '14 at 22:49
  • Ahh yeah, sorry about that... I haven't crawled out of the 2.x stone age yet haha. So you are saying that input() is now the equivalent of the old raw_input()? – J_mie6 Mar 28 '14 at 22:52
  • Don't worry, haven't crawled out of the fuzzy comfort of 2.7 either. Yes, kindly see [this](http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x). – WGS Mar 28 '14 at 22:53
  • Thanks! I should probably migrate to 3.x sometime soon, rather than later ;) – J_mie6 Mar 28 '14 at 22:55
0

One way to check it is to use while path != None.

Code:

print('Enter your name...')
name = input()
print('Welcome to the game, Adventurer ' + name + '!')
path = 'none'
while path != None:
    print('You walk down a corridor, do you go left or right?')
    response = input()
    if 'left' in response:
        print('You take the left turn.')
        path = 'left'
    elif 'right' in response:
        print('You take the right turn.')
        path = 'right'
    else:
        print('Sorry, I didnt understand')
        path = None

Result:

>> nanashi@ubuntu:~/Documents/Python 2.7/Scrapers$ python test9.py
>> Enter your name...
>> Nanashi
>> Welcome to the game, Adventurer Nanashi!
>> You walk down a corridor, do you go left or right?
>> ehhh, what's up, doc?
>> Sorry, I didnt understand
>> nanashi@ubuntu:~/Documents/Python 2.7/Scrapers$ python test9.py
>> Enter your name...
>> Nanashi
>> Welcome to the game, Adventurer Nanashi!
>> You walk down a corridor, do you go left or right?
>> left
>> You take the left turn.
>> You walk down a corridor, do you go left or right?
>> right
>> You take the right turn.
>> You walk down a corridor, do you go left or right?
>> eeeeh, what's up doc?
>> Sorry, I didnt understand

However, the accepted way of doing this is by using while path is not None, as a check versus object identity is better than checking for equality via operands (which can easily be "tricked"). This is as per PEP8 recommendations as well.

WGS
  • 13,969
  • 4
  • 48
  • 51
  • The proper way would be to use `while path is not None:` if using `None`, as from PEP8: "Comparisons to singletons like None should always be done with `is` or `is not`, never the equality operators.". – Leigh Mar 28 '14 at 23:02
  • Hmm. Wording was a bit heavy, but you are correct. Shouldn't have used "proper". :) – WGS Mar 28 '14 at 23:05