重置变量时循环主要功能

时间:2019-04-07 18:06:02

标签: python loops

刚开始在我的计算机基础课程中学习Python。我们必须构建一个程序来创建数学练习集。用户输入两个数字,一个运算符,然后一个解决方案,然后程序会告诉他们它们是否正确。在那之后,该程序应该循环返回,以便用户可以继续练习。该程序应循环执行总共5个练习集。我有一个循环,但是执行它时,它会重复用户最初输入的输入,而不是进行重置,因此用户无法再进行任何输入。我想念什么?

我尝试了一段时间True:在全局变量上循环,但这只会导致main函数在没有完成程序的情况下循环。作业非常清楚地表明需要使用while循环,因为我的教授提供了一些提示来帮助我们。

num1 = int(input("Enter First Input: "))
num2 = int(input("Enter Second Input: "))
op = str(input("Enter Operator: "))
UserSolution = int(input("Enter Solution: "))
res1 = num1+num2
res2 = num1-num2
res3 = num1*num2
res4 = num1/num2
timesCorrect = 0
timesIncorrect = 0

def main ():
    counter = 0
    while counter < 4:      
        print(num1)
        print(num2)
        print(op)
        print(UserSolution)
        counter = counter + 1

函数的确确实像我想要的那样循环,但是它并没有像我想要的那样重置变量。

2 个答案:

答案 0 :(得分:0)

您需要将输入语句移至循环内,例如:

timesCorrect = 0
timesIncorrect = 0

def main ():
    counter = 0
    while counter < 4:
        num1 = int(input("Enter First Input: "))
        num2 = int(input("Enter Second Input: "))
        op = str(input("Enter Operator: "))
        UserSolution = int(input("Enter Solution: "))
        res1 = num1 + num2
        res2 = num1 - num2
        res3 = num1 * num2
        res4 = num1 / num2
        print(num1)
        print(num2)
        print(op)
        print(UserSolution)
        counter = counter + 1

此外,如果您希望它循环五次,则需要将计数器比较更改为< 5<= 4,而不是< 4

答案 1 :(得分:0)

可能会超出您的分配范围,但这是未经测试的建议:

# Mix it up a little by hiding the user's suggested solution with getpass()
from getpass import getpass

### Set iterator variable to avoid hard-coding the script
max_iterations = 5


def evaluate_expression(first_input, second_input, operator):
    """
        Function to handle arithmetic
    """
    my_solution = 0
    if operator == '+':
        my_solution = first_input + second_input
    elif operator == '-':
        my_solution = first_input - second_input
    elif operator == '/':
        # Can't divide by zero, so we have to handle that.
        if second_input != 0:
            my_solution = first_input / second_input
        else:
            my_solution = 0
    elif operator == '*':
        my_solution = first_input * second_input
    return my_solution

def main():

    ### Counters
    correct_guesses = 0
    incorrect_guesses = 0
    try_counter = 1

    while try_counter <= max_iterations:      
        num1 = int(input("Enter First Input: "))
        num2 = int(input("Enter Second Input: "))
        op = str(input("Enter Operator: "))
        UserSolution = int(getpass("Enter Solution: ")) # Hide the input

        ### We van evaluate the expression and return the result to a variable using eval()
        # temp_solution = eval(f'{num1} {op} {num2}')

        ## Traditional evaluation method
        #if op == '+':
        #    my_solution = num1 + num2
        #elif op == '-':
        #    my_solution = num1 - num2
        #elif op == '/':
        #    my_solution = num1 / num2
        #elif op == '*':
        #    my_solution = num1 * num2

        # Call our expression and evaluate the results
        if evaluate_expression(num1, num2, op) == UserSolution:
            print('You\'re correct!')
            correct_guesses += 1
        else:
            print('Incorrect answer!')
            incorrect_guesses += 1

        try_counter += 1
    print(f'Number of correct guesses: {correct_guesses]\nNumber of incorrect guesses: {incorrect_guesses}')