Hello, I'm a beginner using Python 3.3.3. I'm working on a little text-based rpg using the skills I've learned thus far, but am wondering what the problem is with my code. When I enter the first input response, like 1, everything seems to be fine, but when I enter 2, I have to type it twice, and 3, I have to type it in thrice just to get to the next step in the code. The class code is below:

import random

class City(object):
    def __init__(self,name,size):
        self.size = size
        self.name = name
    def cityMenu(self):
        print('What will you do?')
        print('''1 - Visit the tavern
2 - Visit the inn
3 - Visit City Hall''' )
        if input()=='1':#This is fine.
            print('Okay, you\'re heading to {}\'s tavern.'.format(self.name))  

        elif input()=='2':#This has to be typed in twice.
            print('Okay, you\'re heading to {}\'s inn.'.format(self.name))     

        elif input()=='3':#This has to be typed in three times.
            print('Okay, you\'re heading to {}\'s city hall.'.format(self.name))          

        else:#Four times.
            self.cityMenu()

    def exploreCity(self):#maybe use this after a convo at the inn or city hall
        while True:
            print('Explore the outer limits of {}? Y or N?'.format(self.name))
            if input().lower().startswith('y'):#This seems to be fine.
                random.randint(0,10)
                if random.randint(0,10) >= 0:
                    print('Nice.')
                    break
            elif input().lower().startswith('n'):#This has to be typed in twice.
                print('Well, okay then.')
                break
            else:
                print('Please choose a proper command.')

Firhaven = City('Firhaven','The city of fortitude.')
Triae = City('Triae','The city of intellect.')
Cavana = City('Cavana','The city of strength.')
Silvercity = City('Silvercity','The city of light.')

print('You have reached {}, {}'.format(Firhaven.name,Firhaven.size))
Firhaven.cityMenu()
Firhaven.exploreCity()

What is happening here is that you are actually calling the input() operation separately each time you are testing for it, which means that if the value of the first entry is not '1' (and note that it is the string '1', not the numeric literal 1), then the value is discarded - you never save it anywhere. Then, when you get to the first elif clause, you call input() a second time, reading a new value in and testing that. Likewise, the second elif clause reads for a third time, and tests the third value entered.

To avoid this, you want to call input() only once, and save the value in a variable, which you would then test for the three values:

        selection = input()
        if selection == '1':#This is fine.
            print('Okay, you\'re heading to {}\'s tavern.'.format(self.name))  

        elif selection == '2':#This has to be typed in twice.
            print('Okay, you\'re heading to {}\'s inn.'.format(self.name))     

        elif selection == '3':#This has to be typed in three times.
            print('Okay, you\'re heading to {}\'s city hall.'.format(self.name))          

        else:#Four times.
            self.cityMenu()

This should solve the problem nicely.

Of course! Can't believe I forgot about that. Thank you very much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.