Неправильная и правильная функция не будет работать - PullRequest
0 голосов
/ 29 июня 2018

Мой счет не будет распечатываться правильно, просто всегда говорят, что 1 из 6, этот код внизу, как я могу заставить пользователя нажать Enter, и игра выйдет в любой момент в игре, спасибо

score = 0
def strchecker(question):
    valid=False
    while not valid:
        user_Name = input(question)
        if user_Name!="":
            valid=True
            return user_Name
        else:
            print("Please do not leave username blank")

print("*************Welcome to the Te Reo Maori Quiz***************\n"
       "You will be give a series of 6 questions,\nto answer you will enter an answer between 1 and 4.\n\nBest of Luck,and remember if you would like to quit the game just press enter :)\n")

user_Name = strchecker("Please enter your username:\n")

print("Hi", user_Name,"Here is your first question:\n")



#  List of questions in Quiz
question_List = ["How do you write number 1 in Maori?\n1.Tekau 2.Tahi 3.Ono 4.Rua",
                  "What is does tahi + tahi = ?\n1.Rua 2.Rimu 3.Ono 4.Tahi",
                  "How do you write blue in Maori?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
                  "What two colours make blue?\n1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
                  "Who was the god of the forest and birds?\n:1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
                  "Who were Tane Mahutas Parents?\n1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]

# List of Correct Answers
correct_Answer = [2, 1, 2, 2, 3, 4]


# If user enters anything that is not an integer between 1 and 4 it will be an invalid input 
def intcheck(question, low, high):
    valid= False
    while not valid:
        error= "Whoops! Please enter an integer between {} and {}\n".format(low, high)
        try:
            response = int(input("Please enter your answer or press enter to quit\n"))

            if low <= response <= high: 
                return response
            else:
                print(error)
                print()
        except ValueError:
              print(error)


# Get a question from the question list and print and loop one by one 
for idx, question in enumerate(question_List):
    print(question)

    Answer = intcheck("Please enter in an answer or press enter to quit", 1,4)
    print()
# Get answer and check if it is correct or incorrect by going to the list of correct answers 
    if Answer == correct_Answer[idx]:
            print("Well Done, your answer was correct\n")
            score =+1
    else:
         print("Hard Luck, your answer was incorrect\n")

if score <4:
    print("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge.")
elif score >4:
    print("You got",score,"out of 6.\n\nYNice job! Your study payed off

Ответы [ 2 ]

0 голосов
/ 29 июня 2018

, так как ввод для ввода ответов в вашей программе лучше использовать что-то вроде "выйти из игры типа Y или выйти"

проверьте мой код

response = str(input("Please enter your answer or press enter to quit\n"))
            if response == "":
                sys.exit()

            else:
                response = int(response)

попробуйте что-нибудь подобное для выхода из игры

import sys

score = 0
def strchecker(question):
    valid=False
    while not valid:
        user_Name = input(question)
        if user_Name!="":
            valid=True
            return user_Name
        else:
            print("Please do not leave username blank")

print("*************Welcome to the Te Reo Maori Quiz***************\n"
       "You will be give a series of 6 questions,\nto answer you will enter an answer between 1 and 4.\n\nBest of Luck,and remember if you would like to quit the game just press enter :)\n")

user_Name = strchecker("Please enter your username:\n")

print("Hi", user_Name,"Here is your first question:\n")



#  List of questions in Quiz
question_List = ["How do you write number 1 in Maori?\n1.Tekau 2.Tahi 3.Ono 4.Rua",
                  "What is does tahi + tahi = ?\n1.Rua 2.Rimu 3.Ono 4.Tahi",
                  "How do you write blue in Maori?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
                  "What two colours make blue?\n1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
                  "Who was the god of the forest and birds?\n:1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
                  "Who were Tane Mahutas Parents?\n1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]

# List of Correct Answers
correct_Answer = [2, 1, 2, 2, 3, 4]


# If user enters anything that is not an integer between 1 and 4 it will be an invalid input 
def intcheck(question, low, high):
    valid= False
    while not valid:
        error= "Whoops! Please enter an integer between {} and {}\n ".format(low, high)
        try:
            response = str(input("Please enter your answer or press enter to quit\n"))
            if response == "":
                sys.exit()

            else:
                response = int(response)
            if low <= response <= high: 
                return response
            else:
                print(error)
                print()
        except ValueError:
              print(error)


# Get a question from the question list and print and loop one by one 
for idx, question in enumerate(question_List):
    print(question)

    Answer = intcheck("Please enter in an answer or press enter to quit", 1,4)
    print()
# Get answer and check if it is correct or incorrect by going to the list of correct answers 
    if Answer == correct_Answer[idx]:
            print("Well Done, your answer was correct\n")
            score += 1
    else:
         print("Hard Luck, your answer was incorrect\n")

if score <4:
    print("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge.")
elif score >4:
    print("You got",score,"out of 6.\n\nYNice job! Your study payed off")
0 голосов
/ 29 июня 2018

У вас есть простая опечатка. Вы написали - score =+1 вместо score += 1 в нижней части вашего скрипта.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...