我很抱歉这么模糊,但有谁知道为什么这不起作用?

时间:2017-12-10 14:51:42

标签: python python-3.x

抱歉,有谁知道为什么这个程序不起作用?我对python很新(正如你从低效的编程中看到的那样),我想不出有什么理由不起作用。请帮忙。三江源!

#Land Game Calculator

game = 1

hamlet = 0
village = 0
town = 0
city = 0
capital = 0
palace = 0
farm = 0
factory = 0
ruins = 0
apolw = 0
apoll = 0
polw = 0
poll = 0

squaresused = hamlet+village+town+city+capital+palace+farm+factory+ruins+apolw+apoll+polw+poll

income = hamlet+(village*2)+(town*3)+(city*4)+(capital*5)+(palace*6)+farm+(factory*3)


while game == 1:
    if input("What would you like to do? ") == hamlet:
        hamlet = hamlet+(int(input("How many would you like to add? ")))
        print("There are now "+str(hamlet)+" of them")

    elif input("What would you like to do? ") == village:
        village = village+(int(input("How many would you like to add? ")))
        print("There are now "+str(village)+" of them")

    elif input("What would you like to do? ") == town:
        town = town+(int(input("How many would you like to add? ")))
        print("There are now "+str(village)+" of them")

2 个答案:

答案 0 :(得分:0)

问题在于您要将字符串与整数进行比较。 这取决于input行:

if input("What would you like to do? ") == hamlet:

此处hamletint,定义为hamlet = 0。 另一方面,input()会返回strinput返回的值永远不会等于hamlet

我的意思是,我认为,根据用户键入的内容触发不同的操作。 我猜你希望用户输入"hamlet""village""town"。 因此,比较应该在那些字符串而不是变量上:

if input("What would you like to do? ") == "hamlet":

答案 1 :(得分:0)

while循环没有正常工作,但是通过更改每个段落中调用if input...来修复它

command = ""
game = 1
while game == 1:
    game = 1
    command = input("What would you like to do? ")
    if command == hamlet:
        hamlet = hamlet+(int(input("How many would you like to add? ")))
        print("There are now "+str(hamlet)+" of them")
        game = 1

    elif command == village:
        game = 1
        village = village+(int(input("How many would you like to add? ")))
        print("There are now "+str(village)+" of them")
        game = 1