-2

I am doing exercise 36's homework from Learn python 3 the hard way by Zed Shaw. And I am writing Zork-like text adventure. It is in really starting stage. I've tested all of the interactions in the rooms so far and they all work except for one.

I think I am doing a syntax error somewhere, but I can't seem to find where, because I am new to programming.



def room1():
    print('You have found a room with three doors: Left, Middle and  Right')

    choice = input('Where do you want to go?')
    if choice == 'left':
        room3()
    elif choice == 'right':
        room2()
    elif choice == 'middle':
        room5()
    else:
        print('You must pick one of the three')


def room2():
    print('You have found a room with three doors: Left, Middle and  Right')

    choice = input('Where do you want to go?')
    if choice == 'left':
        room1()
    elif choice == 'right':
        room4()
    elif choice == 'middle':
        room5()
    else:
        print('You must pick one of the three')

def room3():
    print('You enter an empty room, you might want to go back')

    choice = input('Do you wish to go back?')
    if choice == 'yes':
        print('You have returned to the previous room')
        room1()


def room4():
    print('You have found a room with a portal')

    choice = input('Take it?')
    if choice == 'yes':
        print('You went through it and now you are in another room')
        room6()
    elif choice == input():
        print('You are still in the same room')
    else:
        print('You must make a decision...')


def room5():
    print('You are now in a room with two doors: Left and right, pick one')

    choice = input('Left or right?')

    if choice == 'left':
        room6()
    if choice == 'right':
        room7()


def room6():
    print('You are in a room with a portal inside and a door.')

    choice = input('Choose portal or door')

    if choice == 'portal':
        room4()
    if choice == 'door':
        broom()


def room7():
    print('You are now in a room with two doors: Left and right, pick one')

    choice = input('Left or right?')

    if choice == 'left':
        room5()
    if choice == 'right':
        broom()


def broom():
    print('You enter a room and you trigger a trap')
    print('Pick your lucky number to see if you dodge the trap')

    choice = input('Lucky number')

    if choice > 100:
        print('GG')

    elif choice < 100:
        print('Death by trap')

    else:
        print('Pick a number!')


def start(): 
    # if start function is on top of code 
    # it wont work cuz it wont find room 1 and 2
    print('This is the start')
    print('You have two doors: Left and Right')

    choice = input ('Where do you want to go?')
    if choice == 'left':
        room1()
    elif choice == 'right':
        room2()
    else:
        dead('You must pick a door')


start()

I expect to pick a number and the program to check if it is greater than 100 and take the corresponding action (print). And then return at room1. But I get error:

Traceback (most recent call last):
  File "pex31.py", line 108, in <module>
    start()
  File "pex31.py", line 103, in start
    room2()
  File "pex31.py", line 24, in room2
    room4()
  File "pex31.py", line 44, in room4
    room6()
  File "pex31.py", line 68, in room6
    broom()
  File "pex31.py", line 86, in broom
    if choice > 100:
Saleem Ali
  • 1,363
  • 11
  • 21
  • 4
    `choice` is of type string. You can't compare it with a integer value. – kuro Sep 17 '19 at 11:32
  • 1
    "I think I am doing a syntax error somewhere, but I can't seem to find where, because I am new to programming."-- you can find exactly where because the traceback includes the line number and actual line. always read the error messages, you'll soon get used to it – Chris_Rands Sep 17 '19 at 11:34

1 Answers1

1

Try:

choice = int(input('Lucky number'))

As Kuro mentioned in comments: the type of your choice input is string, and in your if statement you are comparing it to an integer, so for the comparison to work you must first cast your string input to integer with int().

Also, you might want to surround your choice = int(input('Lucky number')) statement with a try: ... except: ... block to check if the input really was integer, because now if the input cannot be cast to integer your program will crash.

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
Toni Sredanović
  • 2,280
  • 1
  • 11
  • 13
  • 1
    Please mention what was wrong with OP's code. As he is a beginner, pasting the code will not help him – kuro Sep 17 '19 at 11:33
  • Thank you guys for trying to assist me. I think I will skip the try and except suggestion. It is too advanced at this point lol. But I think I did understand my initial mistake. Thanks. – Baboon Prime Sep 17 '19 at 14:05