0

I'm currently working with Python 3.4 on Debian 8.5 linux. I'm working with "Learn Python 3 the Hard way" and I'm working with the section about running test (via nosetests). I have code (which I will include in full) and it's giving me an error on a statement that should be valid, I'm assuming that it's not liking HOW I've set the statement up. It's on the "Class prompt" section:

class prompt(object): prompt = input("> ") 

The idea is to code this prompt one time only and: call it later on for easier coding, however nose keeps responding with :

TypeError: bad argument type for built-in operation

I'll include the full error test as well.

I'm looking to see if in fact, I'm calling this prompt incorrectly and if so, I'd appreciate ONLY A NUDGE in the right direction, not a full solution ( I learn better that way :) ) Thanks!

====== BEGIN engine.py ==============

# babylon 5 scorched Earth # This is a more advanced example of a proof-of-concept I worked on # a few weeks ago. # This time the engine will be all one file, so that the rooms can be # self contained and only need to include 1 file only # This will have only one room, but that room will change # depending on what race you choose! So in this game # there are a total of 5 rooms: # Earther - Earth on the surface # Drakh - Earth, but underground! # Marsie - Mars, just inside the mars dome # Minbari - Minbar # Centari - Centari Prime, in front of the emperor's palace # Stats are part of this model, they are: # CHA - Charisma - how charming and persuasive you are # CON - Constitution - how much damage can YOU take # DEX - How fast are you, how minble are your finges # INT - How smart are you # STR - How strong are you # TECH - NOT RANDOM - Earthers start with 5, everyone else starts with 10 # TEEP - 1 in 100 chance you will be a teep, CANNOT BE CHOSEN!! # WIS - How savy are you, or how wise are you to schemes # The prompt will be defined in this engine too, and called # throught the game # First the imports from sys import exit # We'll call this from the first room from random import randint # for randomizing stats # define names as an object, also define invalid names class name(object): def name(self): invalid.name=("Delenn", "Kosh", "Morden", "Sheridan", "Sinclair", "Ivanova" "G'Kar") # define races - we have to know what races we're working with # define valid races - all other races are excluded class race(object): def race(self): valid.race = ("Centari", "Drakh", "Earther", "Marsie", "Minbari") # Now we define stats - AGAIN TECH AND TEEP ARE NOT RANDOM # Tech is 5 for Earthers and 10 for everyone else # Teep is a random roll from 1 to 100 and cannot be elected # all races are valid for teeps class stats(object): def stats(self): stats = [ " Cha: " " Con: " " Dex: " " Int: " " Str: " " Tech: " " Teep: " " Wis : " ] def cha(self): print(stats.Cha.self[randint(0 - 20)]) def con(self): print(stats.Con.self[randint(0 - 20)]) def dex(self): print(stats.Dex.self[randint(0 - 20)]) def int(self): print(stats.Int.self[randint(0 - 20)]) def str(self): print(stats.Str.self[randint(0 - 20)]) def teep(self): print(stats.Teep.self[randint(0 - 100)]) def wis(self): print(stats.Wis.self[randint(0 - 20)]) def tech(self): if race(self)== "Earther": print(stats.Tech.self("5")) else: print(stats.Tech.self("10")) # Now setting up the prompt class prompt(object): prompt = input("> ") # Now we set up the room - this is the generic code # that tells the program what to do with the room # This code will be really generic, we're just defining # the word "room" , once we get to the engine we'll # be telling WHAT to do with the room class room(object): def enter(self): print("There's no room here at the moment.") print("create a room and it will show something ") print("other than this ! ") exit(1) # Only if there are no other rooms present # Now here's the engine, where we tell it WHAT to do with the # room exactly. basically, we're going to tell it to go to # the current room and look for code to move us to the next room ! class Engine(object): def __init__(self, room_map): self.room_map = room_map def play(self): # needs to be defined as we'll use this to start current_room = self.room_map.opening_room() # the program last_room = self.room_map.next_room('finished') while current_room !=last_room: next_room_name = current_room.enter() currrent_room = self.room.map.next_room(next_room_map) # print out thue room current_room.enter() # Currently there is no death coded in here. # Next up we'll try coding the map class in here , just to see # it the engine can be fully self-contained class Map(object): # Here's where every room is mapped out rooms = { # Here th rooms are officially named ' atr' : Atr(), # first room is at the rim ' centari_prime' : CentariPrime(), # For Centari ' earth_under' : EarthUnderground(), # For Drakh ' earth' : Earth(), # For Earthers ' marsdome' : MarsDome(), # For Marsies ' minbar ' : Minbar(), # For Minbari ' finished' : Finished(), # For the last room! } # Now defining the map itself def __init__(self, start_map): self.start_room = start_rooms def next_room(self, room_name): val = Map.room.get(room_name) return val def opening_room(self): return self.next_ room(self.start_room) # Now defining WHERE the starting room is a_room = rooms('atr') a_game = Engine(a_room) a_game = play() 

===== BEGIN ERROR LOG ========

====================================================================== ERROR: Failure: TypeError (bad argument type for built-in operation) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/nose/failure.py", line 39, in runTest raise self.exc_val.with_traceback(self.tb) File "/usr/local/lib/python3.4/dist-packages/nose/loader.py", line 418, in loadTestsFromName addr.filename, addr.module) File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 47, in importFromPath return self.importFromDir(dir_path, fqname) File "/usr/local/lib/python3.4/dist-packages/nose/importer.py", line 94, in importFromDir mod = load_module(part_fqname, fh, filename, desc) File "/usr/lib/python3.4/imp.py", line 235, in load_module return load_source(name, filename, file) File "/usr/lib/python3.4/imp.py", line 171, in load_source module = methods.load() File "<frozen importlib._bootstrap>", line 1220, in load File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked File "<frozen importlib._bootstrap>", line 1129, in _exec File "<frozen importlib._bootstrap>", line 1471, in exec_module File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed File "/home/phoenix/python3/b5_scorched/projects/skeleton/tests/engine_test.py", line 5, in <module> from b5_scorched.engine import * # import all classes File "/home/phoenix/python3/b5_scorched/projects/skeleton/b5_scorched/engine.py", line 88, in <module> class prompt(object): File "/home/phoenix/python3/b5_scorched/projects/skeleton/b5_scorched/engine.py", line 89, in prompt prompt = input("> ") TypeError: bad argument type for built-in operation ---------------------------------------------------------------------- Ran 1 test in 0.006s FAILED (errors=1) 

Thank you again !

1 Answer 1

1

I can't reproduce your error messages but here's what I see:

This definition is strange:

class prompt(object): prompt = input("> ") 

when the file is run or imported, it prompts for input that's thrown away. Since you don't invoke this feature, it's hard to know how you meant it to be used. Here's my guess at an implementation:

class Prompt(): @staticmethod def input(): return input("> ") 

USAGE

>>> x = Prompt.input() > goat >>> x 'goat' >>> 

Beyond that, there are other problems with the program: this function has unreachable code:

def opening_room(self): return self.next_ room(self.start_room) 

Anything after a return like this is never executed. There's also no import or code in this file to define what Atr() is. Additionally, there seem to be indentation issues (e.g. none of class Map() methods are indented) and static/class methods are defined as instance methods.

You might want to test smaller chucks of code as you build up to larger ones.

Sign up to request clarification or add additional context in comments.

1 Comment

...actually.... I'm intending to write a prompt command that I can reference in later modules. I was answered a bit after I posted this, though the answer's been removed, my error was trying to code the prompt as an object, that's dead wrong. I would have to code it simply as you did "class prompt(): " then indicate this is the first definition of it, then trot out the typical action = input(" > " ). That would work better! Thanks for your reply!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.