我目前正在使用Debian 8.5 linux上的Python 3.4。 我正在使用“学习Python 3艰难的方式”,我正在使用关于运行测试的部分(通过nosetests)。 我有代码(我将完整包含)并且它在一个应该有效的语句中给我一个错误,我假设它不喜欢我如何设置语句。它位于“类提示”部分:
class prompt(object):
prompt = input("> ")
我们的想法是只对此提示进行一次编码,然后:稍后调用它以便于编码,但是鼻子会一直响应:
TypeError:内置操作的错误参数类型
我也会包含完整的错误测试。
我正在寻找是否实际上,我正在调用此提示错误,如果是这样,我会欣赏正确的方向,而不是一个完整的解决方案(我学得更好:) :)谢谢!
====== 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)
再次感谢你!
答案 0 :(得分:1)
我无法重现您的错误消息,但这就是我所看到的:
这个定义很奇怪:
class prompt(object):
prompt = input("> ")
运行或导入文件时,会提示输入被丢弃的输入。由于您没有调用此功能,因此很难知道您的使用方式。以下是我对实施的猜测:
class Prompt():
@staticmethod
def input():
return input("> ")
<强> USAGE 强>
>>> x = Prompt.input()
> goat
>>> x
'goat'
>>>
除此之外,程序还存在其他问题:此功能无法访问代码:
def opening_room(self):
return self.next_
room(self.start_room)
此类return
之后的任何内容都不会被执行。此文件中也没有导入或代码来定义Atr()
。此外,似乎存在缩进问题(例如,没有class Map()
方法缩进),静态/类方法被定义为实例方法。
您可能希望在构建较大代码时测试较小的代码块。