试图让我的python代码询问一定数量的问题,然后停止 [英] Trying to get my python code to ask a set number of questions and then stop

查看:55
本文介绍了试图让我的python代码询问一定数量的问题,然后停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,该代码运行着无数问题,并且除非您选择我包含的退出选项,否则永远不会停止,但是我想摆脱它,因此它仅取决于用户输入的问题数量.我知道我需要一会儿循环,但是我不知道如何正确安装它.请帮忙,这里是代码

At the moment the code runs for an infinite amount of questions and never stops unless you select the exit option I included in but I want to get rid of it so it solely relies on the amount of questions the user inputs. I know I need a while loops but I don't know how to install it properly. Please help here is the code

   import random

def display_intro():
    title = "** A Simple Math Quiz **"
    print("*" * len(title))
    print(title)
    print("*" * len(title))


def display_menu():
    print("Choose your difficulty !")
    menu_list = ["1. Easy", "2. Hard", "3. Spicy", "4. Exit"]
    print(menu_list[0])
    print(menu_list[1])
    print(menu_list[2])
    print(menu_list[3])

def get_user_input():
    user_input = int(input("Enter your choice: "))
    while user_input > 4 or user_input <= 0:
        print("Invalid menu option.")
        user_input = int(input("Please try again: "))
    else:
        return user_input

def num_questions():
   total_questions = int(input("How many questions would you like?: "))
    while total_questions >= 0 and total_questions < 100:
        print("Lets Go!")
        break
    else:
        print ("Please Try Again")

def get_user_solution(problem):
    print("Enter your answer")
    print(problem, end="")
    result = int(input(" = "))
    return result


def check_solution(user_solution, solution, count,):

    if user_solution == solution:
        count = count + 1
        print("Correct.")
       return count
    else:
        print("Incorrect.")
        return count

def menu_option(index, count):
    number_one = random.randrange(1, 9)
    number_two = random.randrange(1, 9)
    number_three = random.randrange(10, 99)
    number_four = random.randrange(10, 99)
    number_five = random.randrange(100, 999)
    number_six = random.randrange(100, 999)

    if index is 1:
        problem = str(number_one) + " * " + str(number_two)
        solution = number_one * number_two
        user_solution = get_user_solution(problem)
        count = check_solution(user_solution, solution, count)
        return count
    elif index is 2:
        problem = str(number_three) + " * " + str(number_four)
        solution = number_three * number_four
        user_solution = get_user_solution(problem)
        count = check_solution(user_solution, solution, count)
        return count
    elif index is 3:
        problem = str(number_five) + " * " + str(number_six)
        solution = number_five * number_six
        user_solution = get_user_solution(problem)
        count = check_solution(user_solution, solution, count)
        return count

def display_result(total, correct):
    if total > 0:
        result = correct / total
        percentage = round((result * 100), 2)
    if total == 0:
        percentage = 0
    print("You answered", total, "questions with", correct, "correct.")
    print("Your score is ", percentage, "%. Thank you.", sep = "")


def main():
    display_intro()
    num_questions()
    display_menu()

    option = get_user_input()
    total = 0
    correct = 0
    while option != 4:
        total = total + 1
        correct = menu_option(option, correct)
        option = get_user_input()

    print("Exit the quiz.")
    display_result(total, correct)

main()

推荐答案

这是解决方案:

import random

total_questions = 0 # make it global

def display_intro():
    title = "** A Simple Math Quiz **"
    print("*" * len(title))
    print(title)
    print("*" * len(title))


def display_menu():
    print("Choose your difficulty !")
    menu_list = ["1. Easy", "2. Hard", "3. Spicy", "4. Exit"]
    print(menu_list[0])
    print(menu_list[1])
    print(menu_list[2])
    print(menu_list[3])

def get_user_input():
    user_input = int(input("Enter your choice: "))
    while user_input > 4 or user_input <= 0:
        print("Invalid menu option.")
        user_input = int(input("Please try again: "))
    else:
        return user_input

def num_questions():
    global total_questions
    total_questions = int(input("How many questions would you like?: "))
    while total_questions >= 0 and total_questions < 100:
        print("Lets Go!")
        break
    else:
        print ("Please Try Again")

def get_user_solution(problem):
    print("Enter your answer")
    print(problem, end="")
    result = int(input(" = "))
    return result


def check_solution(user_solution, solution, count,):

    if user_solution == solution:
        count = count + 1
        print("Correct.")
        return count
    else:
        print("Incorrect.")
        return count

def menu_option(index, count):
    number_one = random.randrange(1, 9)
    number_two = random.randrange(1, 9)
    number_three = random.randrange(10, 99)
    number_four = random.randrange(10, 99)
    number_five = random.randrange(100, 999)
    number_six = random.randrange(100, 999)

    if index is 1:
        problem = str(number_one) + " * " + str(number_two)
        solution = number_one * number_two
        user_solution = get_user_solution(problem)
        count = check_solution(user_solution, solution, count)
        return count
    elif index is 2:
        problem = str(number_three) + " * " + str(number_four)
        solution = number_three * number_four
        user_solution = get_user_solution(problem)
        count = check_solution(user_solution, solution, count)
        return count
    elif index is 3:
        problem = str(number_five) + " * " + str(number_six)
        solution = number_five * number_six
        user_solution = get_user_solution(problem)
        count = check_solution(user_solution, solution, count)
        return count

def display_result(total, correct):
    if total > 0:
        result = correct / total
        percentage = round((result * 100), 2)
    if total == 0:
        percentage = 0
    print("You answered", total, "questions with", correct, "correct.")
    print("Your score is ", percentage, "%. Thank you.", sep = "")


def main():
    display_intro()
    num_questions()
    display_menu()

    option = 0
    total = 0
    correct = 0

    while option != 4 and total < total_questions:
        option = get_user_input()
        correct = menu_option(option, correct)
        total = total + 1

    print("Exit the quiz.")
    display_result(total, correct)

main()

这篇关于试图让我的python代码询问一定数量的问题,然后停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆