我需要为我的高中编程课编写一个垄断游戏。这是我正在编写的代码的一部分,然后我将把它实现到我的其余代码中。我希望它询问输入(brownhouse1),但是当我运行它时,我没有得到任何输出。它可能有一些非常简单的错误,但我不知道是什么导致它什么也不输出。你知道吗
我试过搞乱运营商,改变房子的价值观,它将不会输出任何东西,我不知道我需要改变什么。(也很抱歉格式错误,第一次使用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
问题是
MeditaranianAve.house and BalticAve.house <= 4
的计算结果是0
只要做
MeditaranianAve.house <= 4 and BalticAve.house <= 4
就行了如果你发现自己在改变一些事情,只是为了看看是否有什么不同的事情发生,这往往是一个指标,表明你的方法有某种问题,而备份,以采取更广泛的观点可以帮助。你知道吗
在本例中,您正在为所有者跟踪几个不同的值,如果您需要这些东西,最好使用一个可以处理自己名称等的对象。你知道吗
我觉得你对比较逻辑语句的工作原理有点困惑。你的第一个
if
语句测试MeditaranianAve.ownername
的存在,然后测试BalticAve.ownername
是否等于“Player2”。如果要检查它们是否都等于该值:不过,通过使用可以有自己方法的对象跟踪更多的状态,您可以清除很多问题。这样,您就可以将与特定类型相关的逻辑放在它自己的位置上。你知道吗
这也许是一个开始。它添加了一些可能不熟悉的关系逻辑,但浏览它将向您展示一种在Python中处理相关数据模型和分离数据类型关注点的通用方法。你知道吗
相关问题 更多 >
编程相关推荐