希望为数学游戏压缩Python代码(对Python来说非常陌生)

问题描述 投票:0回答:1
import random

correct_g = 0
incorrect_g = 0

while True:
    user_input = input('Would you like to add, sub, multi? type "end" to end the game : ').lower()

    if user_input == 'add':
        number1: int = random.randrange(1, 100)
        number2: int = random.randrange(1, 100)
        add_answer = number1 + number2
        print(number1, ' + ', number2)
        add_question = input('What is the sum of these two numbers? ')
        if int(add_question) == add_answer:
            print('That is correct!')
            correct_g += 1
            pass

        else:
            print('Incorrect. whomp whomp:(')
            incorrect_g += 1
            pass

    elif user_input == 'sub':
        number3: int = random.randrange(1, 100)
        number4: int = random.randrange(1, 100)
        sub_answer = number3 - number4
        print(number3, ' - ', number4)
        sub_question = input('What is the sum of these two numbers? ')
        if int(sub_question) == sub_answer:
            print('That is correct!')
            correct_g += 1
            pass

        else:
            print('Incorrect. whomp whomp:(')
            incorrect_g += 1
            pass


    elif user_input == 'multi':
        number5: int = random.randrange(1, 100)
        number6: int = random.randrange(1, 100)
        multi_answer = number5 * number6
        print(number5, ' * ', number6)
        multi_question = input('What is the sum of these two numbers? ')
        if int(multi_question) == multi_answer:
            print('That is correct!')
            correct_g += 1
            pass

        else:
            print('Incorrect. whomp whomp:(')
            incorrect_g += 1
            pass

    elif user_input == 'end':
        print('You got ', correct_g, ' questions right!')
        print('You got ', incorrect_g, ' questions wrong.')
        print('Thank you for playing!')
        break

    else:
        print('I do not understand.')
        pass

我正在寻找压缩一些 if、elif 和数字生成器的方法。我想复制/粘贴并更改一些单词,如果从第一个用户输入创建分支的方法非常低效。我不知道还能怎么做,如果有人推荐一个课程供我查找和学习,我愿意从头开始。

python pycharm
1个回答
0
投票

无论选择如何,您都会创建两个 1,100 之间的随机数。为什么不在选择之前创建它们并始终使用 number1 和 number2 呢?你是对的,复制和粘贴代码通常是一个坏兆头。

您可以创建一个更灵活的函数,并将选择作为输入来创建分支。

import random

def branch(sign):
    if sign=="end":
        return -1
    global correct_g, incorrect_g
    number1 = random.randrange(1, 100)
    number2 = random.randrange(1, 100)
    
    def sub(a, b):
        return a - b

    def add(a, b):
        return a + b

    def multi(a, b):
        return a * b

    # Functions as dict values
    sign2func = {
        "sub": sub,
        "add": add,
        "multi": multi
    }
    
    sign2symbol = {
        "sub": "-",
        "add": "+",
        "multi": "*"
    }
    
    sign2operation = {
        "sub": "difference",
        "add": "sum",
        "multi": "product"
    }

    # Call the appropriate function from the dictionary
    sub_answer = sign2func[sign](number1, number2)

    print(f"{number1} {sign2symbol[sign]} {number2}")
    sub_question = input(f'What is the {sign2operation[sign]} of these two numbers? ')
    
    if int(sub_question) == sub_answer:
        print('That is correct!')
        correct_g += 1
    else:
        print('Incorrect. whomp whomp:(')
        incorrect_g += 1

correct_g = 0
incorrect_g = 0

while True:
    user_input = input('Would you like to add, sub, multi? Type "end" to end the game: ').lower()
    print(user_input)
    if user_input == "end":
        break
    try:
        res=branch(user_input)
        if res==-1:
            break
    except KeyError:
        print('I do not understand.')
© www.soinside.com 2019 - 2024. All rights reserved.