簡體   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