0

I am trying to make a very simple branching story using a class, called Choices, and my main. Choices holds methods for each choice made, and each branch off has its own method, to keep things organized. My main evaluates the previous choice to decide what should run next. Everything returns; but the methods won't run when set to a variable! Everything I've looked at has been about more advanced programs; mine is a simple error that needs correcting. If anyone could tell me why, I'd be very grateful!

main.py:

import Choices

game = Choices.Choices()

reply = game.c()

if reply == "1":
  reply = game.c1()
elif reply == "2":
  reply = game.c2()

Choices.py:

class Choices:
  answer = False

  #Constructor
  def __init__(self):
    self.answer = False

  #Initial Choice
  def c(self):
    while self.answer == False:

      print "What would you like to do?" #Question
      reply = input("[1] Wake up [2] Sleep in") #Answers

      if reply == "1": #Choices
        print "You get up." #Actions
        self.answer = True
        return reply

      elif reply == "2": #Choices
        print "You sleep more." #Actions
        self.answer = True
        return reply

      else:
        print "Not a choice."
        self.answer = False

    self.answer = False

  #Branch 1
  def c1(self):
    while self.answer == False:

      print "What would you like to do?" #Question
      reply = input("[1] Make Breakfast [2] Get Dressed") #Answers

      if reply == "1": #Choices
        print "You go to the kitchen and make some oatmeal." #Actions
        self.answer = True
        return reply

      elif reply == "2": #Choices
        print "You go to the closet and put on some day clothes." #Actions
        self.answer = True
        return reply

      else:
        print "Not a choice."
        self.answer = False

    self.answer = False

  #Branch 2
  def c2(self):
    while self.answer == False:

      print "What would you like to do?" #Question
      reply = input("[1] Wake up [2] Dream") #Answers

      if reply == "1": #Choices
        print "You get up." #Actions
        self.answer = True
        return reply

      elif reply == "2": #Choices
        print "You begin to dream. You are wandering in a forest, when you come to a crossroads..." #Actions
        self.answer = True
        return reply

      else:
        print "Not a choice."
        self.answer = False

    self.answer = False
  • 2
    I don't understand what you mean by " Everything returns; but the methods won't run when set to a variable! ". Can you clarify what is your problem ? – Axnyff Aug 25 '18 at 15:39
  • If you're using Python 2.x - your print statements would imply that's the case... then `input` evaluates the text entered so - entering one will return an integer of 1 which isn't equal to a string of one.... switch to using `raw_input` instead of `input` - it's safer and will work. – Jon Clements Aug 25 '18 at 15:47

2 Answers2

0

I changed your code of Choices.py

class Choices:
    answer = False

    #Constructor
    def __init__(self):
        self.answer = False

    #Initial Choice
    def c(self):
        self.answer = False
        while True:
            print "What would you like to do?" #Question
            reply = raw_input("[1] Make Breakfast [2] Get Dressed") #Answers

            if str(reply) == "1": #Choices
                print "You get up." #Actions
                self.answer = True
                return reply

            elif reply == "2": #Choices
                print "You sleep more." #Actions
                self.answer = True
                return reply

            else:
                print "Not a choice."


    #Branch 1
    def c1(self):
        self.answer = False
        while True:
            print "What would you like to do?" #Question
            reply = raw_input("[1] Make Breakfast [2] Get Dressed") #Answers

            if reply == "1": #Choices
                print "You go to the kitchen and make some oatmeal." #Actions
                self.answer = True
                return reply

            elif reply == "2": #Choices
                print "You go to the closet and put on some day clothes." #Actions
                self.answer = True
                return reply

            else:
                print "Not a choice."

    #Branch 2
    def c2(self):
        self.answer = False
        while True:
            print "What would you like to do?" #Question
            reply = raw_input("[1] Make Breakfast [2] Get Dressed") #Answers
            if reply == "1": #Choices
                print "You get up." #Actions
                self.answer = True
                return reply

            elif reply == "2": #Choices
                print "You begin to dream. You are wandering in a forest, when you come to a crossroads..." #Actions
                self.answer = True
                return reply

            else:
                print "Not a choice."

The first problem is the input,

raw_input() treats all input as a string and returns the string type.

input() has its own characteristics when dealing with pure numeric input, and it returns the type of the input number ( int, float ).

You can't compare an Interger with a string.

The second problem is when you finish one method and come to c1 or c2, it will not run because before you return reply your answer is always True, So in the next method while self.answer == False is equal to while False and it will just do nothing.

Henry
  • 176
  • 6
0

The problem is happening because you are using the function input and assuming its return value to be a string.

This is not true in case of python 2.x

Read this answer to understand more.

anas17in
  • 161
  • 9