繁体   English   中英

Python“更高/更低”游戏无法正常运行

[英]Python “higher/lower” game not working as expected

游戏的工作方式是这样的:向用户提供一个Google搜索字词,例如:“豪华轿车”,然后对该字词进行每月一次搜索,然后向其显示另一个字词。 用户必须猜测后一项是否具有较高或较低的搜索次数才能获得分数。 运行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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM