Problems with Player Input to Select Option

Problems with Player Input to Select Option

Postby Velcrode » Sun Jun 30, 2013 1:18 am

This is a separate inquiry I have for the RPG I'm working on. I'm working on a construction function to add a bit more to do in the game. The system I'm working on isn't exactly the sleekest way to delineate this function, I'm sure, but it's what I've got so far.

My problem now lies in the inputting of a number by the player to select a structure to build. This failure to register the number as input and subsequent error message pictured here:

Code: Select all
if input() == 1:
  File "<string>", line 0
   
    ^
SyntaxError: unexpected EOF while parsing


seems to be an easy fix. That said, could anyone help me understand how to fix it? Here's the code:

Code: Select all
def build(self):
    if self.state == 'normal':
      print('You come to a small clearing. With a bit of time, you could build something.')
      print ('What would you like to build? (type bhelp for building help)')
      project = input()
      if input() == 1:
        while self.construction_level >= 1 and self.stone >= 5:
          print "You build a well. (-5 Stone)"
          self.stone = self.stone - 5
          if randint(0,10) > 8:
            self.construction_level = self.construction_level + 1
            print "Your construction skill leveled up!"
        else:
          return False
      elif input() == 2:
        while self.construction_level >= 1 and self.wood >= 20:
          print "You build a hut. (-20 Wood)"
          self.wood = self.wood - 20
          if randint(0,10) > 8:
            self.construction_level = self.construction_level + 1
            print "Your construction skill leveled up!"
        else:
          return False
      elif input() == 4:
        while self.construction_level >= 2 and self.wood >= 35:
          print "You build a barracks. (-35 Wood)"
          self.wood = self.wood - 35
          if randint(0,6) > 4:
            self.construction_level = self.construction_level + 1
            print "Your construction skill leveled up!"
        else:
          return False
      elif input() == 3:
        while self.construction_level >= 2 and self.stone >= 25:
          print "You build a stone hut. (-25 Stone)"
          self.stone = self.stone - 20
          if randint(0,6) > 4:
            self.construction_level = self.construction_level + 1
            print "Your construction skill leveled up!"
        else:
          return False
      elif input() == 5:
        while self.construction_level >= 3 and self.wood >= 50:
          print "You build a house. (-50 Wood)"
          self.wood = self.wood - 50
          if randint(0,1):
            self.construction_level = self.construction_level + 1
            print "Your construction skill has leveled up!"
          else:
            return False
      elif input() != num(1,5):
        print "Please type a number between 1 and 5."
      else:
        print "You don't have a high enough construction level or enough materials to make this. Check your construction level and materials with the bstat command."
Velcrode
 
Posts: 12
Joined: Sat Jun 29, 2013 3:41 am

Re: Problems with Player Input to Select Option

Postby metulburr » Sun Jun 30, 2013 1:31 am

Code: Select all
      project = input()
      if input() == 1:

umm, you mean
Code: Select all
 if project == 1:
?

by the way project will never equal to 1, as input returns a string. it must be converted to int first and hten test, or just test with the string '1' instead.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Problems with Player Input to Select Option

Postby Velcrode » Sun Jun 30, 2013 1:51 am

Thanks metulburr! I am not an inherently logical being. I changed the code to this:

Code: Select all
      project = input()
      project == int(1,5)
      if project == 1:


and now my error message reads:

Code: Select all
in build
    project == int(1,5)
TypeError: int() can't convert non-string with explicit base


Do people still award others the internets? If so, I do so for you.
Velcrode
 
Posts: 12
Joined: Sat Jun 29, 2013 3:41 am

Re: Problems with Player Input to Select Option

Postby Mekire » Sun Jun 30, 2013 4:30 am

I know it is very easy to think you need these if/else blocks but again it can be avoided here.
Also as soon as you notice yourself typing very similar code over and over, you need to try to think of how a function could be used.

This is still a bit messy but shows the idea. (code is python 2.x. Replace raw_input with input for python 3):
Code: Select all
import random

#id : [thing,min_level,material,amount,level_up_chance]
BUILD_DICT = {"1" : ["well",1,"stone",5,0.2],
              "2" : ["hut",1,"wood",20,0.2],
              "3" : ["stone hut",2,"stone",20,0.33],
              "4" : ["barracks",2,"wood",35,0.33],
              "5" : ["house",3,"wood",50,0.5]}

class Play(object):
    def __init__(self):
        self.state = "normal"
        self.done = False
        self.construction_level = 1
        self.resources = {"stone": 500,
                          "wood" : 500}

    def current_supply(self):
        print("{:<10}   {}".format("Material","Quantity"))
        for material in sorted(self.resources):
            print("{:<10} : {}".format(material.capitalize(),self.resources[material]))
        print("\nConstruction Level: {}\n".format(self.construction_level))

    def choose(self):
        self.current_supply()
        if self.state == 'normal':
            print('\nYou come to a small clearing. With a bit of time, you could build something.')
            print ('What would you like to build?\n')
            for structure in sorted(BUILD_DICT):
                thing,level,material,amount,lev_up = BUILD_DICT[structure]
                args = [structure]+[thing.capitalize(),level,amount,material]
                print("[{}] : {:<10} -- Minimum Level: {}, Needs: {:>3} {}".format(*args))
            project = None
            while project not in BUILD_DICT:
                if project != None:
                    print("Invalid choice.")
                project = raw_input("Please select from the menu. (leave empty to exit)")
                if project == "":
                    self.done = True
                    break
            else:
                self.build_it(project)

    def build_it(self,project):
        thing,min_level,material,amount,level_up_chance = BUILD_DICT[project]
        if self.construction_level >= min_level:
            if self.resources[material] >= amount:
                print("You build a {}. (-{} {})\n".format(thing,amount,material))
                self.resources[material] -= amount
                if random.random() < level_up_chance:
                    self.construction_level += 1
                    print("Your construction skill leveled up!\n")
            else:
                print("You dont not have enough {}.\n".format(material))
        else:
            print("Your construction level is too low. ({} required)\n".format(min_level))

if __name__ == '__main__':
    RunIt = Play()
    while not RunIt.done:
        RunIt.choose()

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Problems with Player Input to Select Option

Postby Velcrode » Sun Jun 30, 2013 5:29 am

That is essentially what I wanted to figure out but had no real inclination of where to start. Print thank you for an infinite loop Mekire and metulburr! I award you both one whole internets. However, implementing the code you posted results in:

Code: Select all
in <module>
    Commands[c](p)
TypeError: build() takes exactly 2 arguments (1 given)


Is this an error with the commands module? Or...??
Velcrode
 
Posts: 12
Joined: Sat Jun 29, 2013 3:41 am

Re: Problems with Player Input to Select Option

Postby Mekire » Sun Jun 30, 2013 6:46 am

My build_it function does indeed require two arguments. One is the instance of the class and the other is the user_input.

IE:
Code: Select all
self.build_it(project)
which is of course equivalent to:
Code: Select all
Play.build_it(self,project)
in case you were wondering why that counts as two arguments.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Problems with Player Input to Select Option

Postby Velcrode » Sun Jun 30, 2013 7:29 am

Code: Select all
File "/Users/USER/CODE/duderpg2.py", line 109, in <module>
    RunIt.choose()
  File "/Users/USER/CODE/duderpg2.py", line 74, in choose
    self.current_supply()
  File "/Users/USER/CODE/duderpg2.py", line 69, in current_supply
    print("{:<10}   {}".format("Material","Quantity"))
ValueError: zero length field name in format


What would I do in this case?
Velcrode
 
Posts: 12
Joined: Sat Jun 29, 2013 3:41 am

Re: Problems with Player Input to Select Option

Postby Mekire » Sun Jun 30, 2013 7:37 am

You are using an old version of python, try this:
Code: Select all
print("{0:<10}   {1}".format("Material","Quantity"))

You will have to do similar for all of my other format statements too.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Problems with Player Input to Select Option

Postby Velcrode » Sun Jun 30, 2013 7:43 am

How does this relate to the method of calling a globally/locally defined string or digit with "%s" or "%d"?
Velcrode
 
Posts: 12
Joined: Sat Jun 29, 2013 3:41 am


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests