gpt4 book ai didi

python - 学校垄断作业的一部分不会输出任何内容(初学者,Python)

转载 作者:行者123 更新时间:2023-12-01 07:52:03 25 4
gpt4 key购买 nike

我需要为我的高中编程课编写一个大富翁游戏。这是我正在处理的代码的一部分,然后我将把它实现到我的其余代码中。我期望它询问输入(brownhouse1),但是当我运行它时,我没有得到任何输出。它可能有一些非常简单的错误,但我无法弄清楚是什么导致它不输出任何内容。

我尝试过与运算符(operator)打交道,更改房屋值(value),但它不会输出任何内容,我不确定我需要更改什么。 (对于第一次使用 stackoverflow,格式错误也深表歉意。)

谢谢!

class Properties:
def __init__(self,name,qprice,qrent,owner,ownername,color,house):
self.name = name
self.price = int(qprice)
self.rent = int(qrent)
self.owner = owner
self.ownername = ownername
self.color = color
self.house = int(house)

MeditaranianAve = Properties("MeditaranianAve",60,2,"Yes","Player2","Brown",0)
BalticAve = Properties("Baltic Ave",60,4,"Yes","Player2","Brown",1)
Brown = (MeditaranianAve, BalticAve)

if MeditaranianAve.ownername and BalticAve.ownername == "Player2":
if MeditaranianAve.house and BalticAve.house <= 4:
brownhouse1 = input("Would you like to buy a house? ")
if brownhouse1 == "y":
if BalticAve.house > MeditaranianAve.house:
Med1 = input("You own more houses on Baltic Ave than Meditaranian Ave, you can only purchase a house for Meditaranian Ave, would you like to purchase? y/n?")
if Med1 == "y":
MeditaranianAve.house = MeditaranianAve.house+1
if MeditaranianAve.house > BalticAve.house:
Balt1 = input("You own more houses on Meditaranian Ave than Baltic Ave, you can only purchase a house for Baltic Ave, would you like to purchase? y/n?")
if Balt1 == "y":
BalticAve.house = BalticAve.house+1

最佳答案

如果您发现自己只是为了看看是否会发生任何不同的情况而改变一些事情,这通常表明您的方法存在某种问题,并且支持更广泛的视野会有所帮助。

在这种情况下,您正在跟踪所有者的多个不同值,而如果您需要这些东西,您可能最好使用可以处理其自己的名称等的单个对象。

我认为您对比较逻辑语句的工作原理有点困惑。您的第一个 if 语句测试 MeditaranianAve.ownername存在,然后测试 BalticAve.ownername 是否等于“Player2” 。如果您想检查它们是否都等于该值:

if MeditaranianAve.ownername == BalticAve.ownername == "Player2":
# ...

不过,您可以通过使用可以拥有自己的方法的对象跟踪更多状态来清理这个问题。这样您就可以将与特定类型相关的逻辑放在自己的位置。

这可能是一个起点。它添加了一些可能不熟悉的关系逻辑,但通过它将会向您展示 Python 中相关数据模型和关注点分离的常用方法。

class PropertyGroup(object):
def __init__(self, name):
self.name = name
self.properties = []

def fully_owned_by(self, player):
for property in self.properties:
if property.owner != player:
return False
return True


class Property(object):
def __init__(self, name, group):
self.name = name
# This assumes all properties are part of a group; otherwise,
# default to None and check whether this exists before trying
# to access it.
self.group = group
group.properties.append(self)
self.owner = None
self.house_count = 0

def houses_available(self):
return self.house_count < 5

def purchase_house(self):
# This is where you'd check to make sure the owner exists and
# can purchase a house, then implement that logic.
pass


class Player(object):
def __init__(self, name):
self.name = name


player = Player('Maddie')

group = PropertyGroup('B&M')
baltic = Property('Baltic Avenue', group)
mediterranean = Property('Mediterranean Avenue', group)

baltic.owner = player

# Since Baltic but not Mediterranean is owned by the player, this
# check will be False.
if baltic.group.fully_owned_by(player) and baltic.houses_available():
print('Prompt for house purchase')
else:
print('No dice.')

mediterranean.owner = player

# Now that both are player-owned, this is True. Note that baltic.group,
# mediterranean.group, and our local variable group all reference the
# same group object, so you could call that method on any of them here.
if mediterranean.group.fully_owned_by(player) and mediterranean.houses_available():
print('Prompt for house purchase')
else:
print('No dice.')

关于python - 学校垄断作业的一部分不会输出任何内容(初学者,Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56159559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com