Majdinel Hima
Majdinel Hima

Reputation: 3

Quiz Game . If not executing

The problem is that it does not execute the last part of code when the user gives as input "ok",any ideas?. I try also to make it def and call it but the same happened. I am stuck. BTW this if is supposed to give you the choice to choose a different category when you give like an input "ok" if endgame.lower()=="ok":. Thanks a lot for your time.

import requests
import json
import pprint
import random
import html
correctAnswers=0
wrongAnswers=0
input("this is a quiz game,do you want to play? Press enter to start or quit to stop ")
endgame=" "
x=True
url=0
while x==True:
    i=1
    categorys=["Geography","History","Sports","Animals"]
    for category in categorys:
        print(str(i) + "- " + category)
        i+=1
    choise=input("Choose a category: ")
    urlChoise=choise
    choise = category[int(choise) - 1]
    if urlChoise=='1':
        url="https://opentdb.com/api.php?amount=1&category=22"
    elif urlChoise=='2':
        url="https://opentdb.com/api.php?amount=1&category=23"
    elif urlChoise=='3':
        url="https://opentdb.com/api.php?amount=1&category=21&type=multiple"
    elif urlChoise=='4':
        url="https://opentdb.com/api.php?amount=1&category=27"
    x = False

while (endgame.lower()!="quit"):
    r= requests.get(url)
    question=json.loads(r.text)
    if (r.status_code!=200):
        endgame=input("Sorry we run to a problem please try again or type quit ")
    else:
        data = json.loads(r.text)
        print(html.unescape(question['results'][0]['question']))
        print("---------------------------------")
        answers = data['results'][0]['incorrect_answers']
        correct_answer = data['results'][0]['correct_answer']
        answers.append(correct_answer)
        answer_number=1
        random.shuffle(answers)

        for answer in answers:
            print(str(answer_number) + "- " + html.unescape(answer))
            answer_number += 1



        useranswer=input("give your answer only with numbers: ")
        useranswer = answers[int(useranswer) - 1]

        if (useranswer== correct_answer):
            print("---------------------------------")

            print("you are right")
            print("---------------------------------")
            correctAnswers+=1
            print("Correct Answers: ", correctAnswers, "\n Wrong Answers: ", wrongAnswers)
            endgame=input("if you want to continiu press enter else write quit if you want to change category write ok: ")
        else:
            print("---------------------------------")
            wrongAnswers+=1
            print("wrong answer the correct one is "+correct_answer)
            print("---------------------------------")
            print("Correct Answers: ",correctAnswers,"\n Wrong Answers: ",wrongAnswers)
            endgame=input("if you want to continiou press enter else write quit if you want to change category write ok: ")
    if endgame.lower()=="ok":
        x==True
        while x == True:
            i = 1
            categorys = ["Geography", "History", "Sports", "Animals"]
            for category in categorys:
                print(str(i) + "- " + category)
                i += 1
            choise = input("Choose a category: ")
            urlChoise = choise
            choise = category[int(choise) - 1]
            if urlChoise == '1':
                url = "https://opentdb.com/api.php?amount=1&category=22"
            elif urlChoise == '2':
                url = "https://opentdb.com/api.php?amount=1&category=23"
            elif urlChoise == '3':
                url = "https://opentdb.com/api.php?amount=1&category=21&type=multiple"
            elif urlChoise == '4':
                url = "https://opentdb.com/api.php?amount=1&category=27"
            x = False```


Upvotes: 0

Views: 49

Answers (1)

Aleksander Ikleiw
Aleksander Ikleiw

Reputation: 2685

the problem is with the

if endgame.lower()=="ok":
    x==True

You do not set the x as True. You are asking the program whether the x is True. You have to change it into x = True

Upvotes: 1

Related Questions