重置变量时循环主函数

2024-10-06 06:51:17 发布

您现在位置:Python中文网/ 问答频道 /正文

刚开始在我的计算基础课上学习Python。我们必须建立一个为数学创造练习集的程序。用户输入两个数字,一个运算符,然后是一个解,程序会告诉他们是否正确。在那之后,程序应该循环,这样用户就可以继续练习了。这个程序应该循环5套练习。我有这个循环,但是当它被执行时,它会重复用户最初输入的输入,而不是重置,因此用户不能再进行任何输入。我错过了什么

我尝试了一段时间:对我的全局变量进行循环,但这只会导致主函数在没有完成程序的情况下循环。作业非常清楚地指出需要使用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

函数确实像我希望的那样循环,但是它没有像我希望的那样重置变量


Tags: 函数用户程序inputcounterint重置print
2条回答

可能会超出你的任务范围,但这里有一个未经验证的建议:

# 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}')

您需要将输入语句移动到循环中,例如:

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

相关问题 更多 >