While 循环重复游戏

While loop repeat for Game

提问人:Yas 提问时间:11/16/2023 更新时间:11/16/2023 访问量:57

问:

我正在尝试让我的游戏将“正确”一词返回给用户。然而,在给出正确答案后,while 循环停止。我如何获得它,以便在答案为“正确”时继续并中断 这是代码的第一部分

#imported libraries
from bs4 import BeautifulSoup 
import requests 
import pandas as pd
from io import StringIO 
import random 

url = 'https://en.wikipedia.org/wiki/List_of_most-followed_Instagram_accounts'

page = requests.get(url)
soup = BeautifulSoup(StringIO(page.text), 'html') 
#clean table and dataset
for t in soup.select('[title="Yes"]'):
    t.replace_with("Yes")
df = pd.read_html(page.text)[0]
df = df.drop(index=(50))
df['Followers (millions)'] = df['Followers (millions)'].astype(float)
#List of names
column_names = list(df.columns)
#While loop to run the game
answer = ''
count  = 0

这是用于运行游戏的 While 循环

while True:
    count+=1
    if count == 2:
        break 
    index = 0
    def randomIndexValue(): #random Index Value generator
        global index 
        index = random.randint(1,49)
        return(index)


    #Select and print Neymar, a footballer, from brasil.
    #have one stament w followers and compare it to another 
    randomIndexValue()
    item_A = [df.iloc[index][column_names[1]], df.iloc[index][column_names[2]], df.iloc[index][column_names[5]], df.iloc[index][column_names[6]], df.iloc[index][column_names[4]]]
    print(f'Compare A: {item_A[1]} is a {item_A[2]} from {item_A[3]} and has {item_A[4]} million followers')

    randomIndexValue()
    item_B= [df.iloc[index][column_names[1]], df.iloc[index][column_names[2]], df.iloc[index][column_names[5]], df.iloc[index][column_names[6]], df.iloc[index][column_names[4]]]
    print(f'Compare B: {item_B[1]} is a {item_B[2]} from {item_B[3]} and has {item_B[4]} million followers')
# Check which comparison is more popular 
    if max(item_A[4],item_B[4]) == item_A[4]:
        answer = 'A'
        print(f'item A which is {item_A[1]} has more followers than item B {item_B[1]}') 
    elif max(item_A[4],item_B[4]) == item_B[4]: 
        answer = 'B'
        print(f'item B which is {item_B[1]} has more followers than item A {item_A[1]}')
    
    score = 0
    question = input('Which option is more popular?\nA or B: ').upper()    
    
    if question == answer: 
        print('Correct!')
        score += 1 * 100
        continue
    elif question != answer: 
        print("Incorrect")

print(f'you scored: {score}')      

我尝试编辑IF语句以添加“continue”,但是同样的问题仍然存在。

    if question == answer: 
        print('Correct!')
        score += 1 * 100
        continue
    elif question != answer: 
        print("Incorrect")
python pandas 数据帧 while-loop

评论


答: 暂无答案