Python"更高/更低"游戏没有按预期工作

时间:2018-06-18 10:20:26

标签: python csv

游戏的工作方式如下:用户获得了一个谷歌搜索词,如:"豪华轿车"并且该术语每月搜索一次,然后再显示另一个术语。用户必须猜测后一项是否具有更高或更低的搜索次数以便接收点。运行python脚本时,我遇到了令人困惑的行为。即使答案是对的,游戏也会停止。

import pandas
from random import choice, randint

columnNames = ['search','number_of_searches']
search_registry = pandas.read_csv('data.csv', names = columnNames)

keywords = search_registry.search.tolist()
values = search_registry.number_of_searches.tolist()

points = 0
stop = 0

while stop != 1:

    randomIndex_1 = randint(1,len(keywords)-1)
    print ( keywords[randomIndex_1] + " has {} monthly global searches ".format(values[randomIndex_1]) )

    randomIndex_2 = choice([i for i in range(1,len(keywords)) if i != randomIndex_1])
    print ( "How many does {} have? Higher or Lower?".format(keywords[randomIndex_2]))

    ans = input()

    if ans == 'higher':
        if values[randomIndex_1] < values[randomIndex_2]:
            points+=1
        elif values[randomIndex_1] > values[randomIndex_2]:
            print("GAME OVER. You got {} points".format(points))
            stop=1

    if ans == 'lower':
        if values[randomIndex_1] > values[randomIndex_2]:
            points+=1
        elif values[randomIndex_1] < values[randomIndex_2]:
            print("GAME OVER. You got {} points".format(points))
            stop=1

这是CSV数据:

"search","number_of_searches"
"Rolex",2740000,
"April The Giraffe",1500000,
"Scarlet Johansson",3350000,
"Maldives",1220000,
"Fargo",823000,
"Profiteroles",201000,
"Road Runner",369000,
"Limousine",246000,
"Birthday Cake",2250000,
"The Secret",301000,
"Ralph Lauren",2240000,
"The Silence Of The Lambs",368000

1 个答案:

答案 0 :(得分:0)

解决了问题。 values[]列表存储str类型而非int的数据。只需在比较时写int(values[])即可解决问题。现在比较是在int类型之间进行的,而不是在字符串之间。