错误和正确的功能将不起作用

时间:2018-06-29 07:06:55

标签: python python-3.x function

我的分数无法正确打印出来,只是总是说6之1是该代码的底部,我又如何让用户按下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 :(得分:1)

您有一个简单的错字。您在脚本底部附近写了score =+1,而不是score += 1

答案 1 :(得分:0)

因为回车用于在程序中输入答案,所以最好使用“退出游戏类型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")