【问题标题】:object oriented python 3 input error面向对象的python 3输入错误
【发布时间】:2017-08-18 03:19:50
【问题描述】:

我目前在 Debian 8.5 linux 上使用 Python 3.4。 我正在使用“Learn Python 3 the Hard way”,并且正在使用有关运行测试的部分(通过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()

===== 开始错误日志 ========

======================================================================
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)

再次感谢您!

【问题讨论】:

    标签: python input python-3.4 prompt


    【解决方案1】:

    我无法重现您的错误消息,但这是我看到的:

    这个定义很奇怪:

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

    当文件运行或导入时,它会提示输入被丢弃的输入。由于您不调用此功能,因此很难知道您打算如何使用它。这是我对实现的猜测:

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

    用法

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

    除此之外,程序还有其他问题:这个函数有无法访问的代码:

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

    return 之后的任何内容都不会执行。此文件中也没有导入或代码来定义 Atr() 是什么。此外,似乎存在缩进问题(例如,class Map() 方法都没有缩进)并且静态/类方法被定义为实例方法。

    您可能希望在构建较大的代码时测试较小的代码块。

    【讨论】:

    • ...实际上....我打算编写一个提示命令,可以在以后的模块中引用。发布此消息后,我得到了一点回答,尽管答案已被删除,但我的错误是尝试将提示编码为对象,这是完全错误的。我必须像你做的那样简单地编码它“类提示():”然后指出这是它的第一个定义,然后小跑出典型的动作=输入(“>”)。那会更好!感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多