如何在程序运行期间随机显示每个“初学者”[关闭]

问题描述 投票:-7回答:1

我目前正在创建一个数学游戏,包括:加法,减法,乘法和除法。这些带有紫色边框的部分是当用户选择添加时我为添加部分创建的问题。

我不知道如何随机显示这些问题。当用户每次进行添加问题时都选择添加,或者在完成后再次进行添加,我希望每次我希望它们的顺序不同时,问题不一样。所以每次都是随机的。

#addition questions
def beginnerquestionsaddition():
    os.system('clear')
    score = 0
    beginner1 = input("2 + 3 = ")
    if beginner1 == ("5"):
        print("Correct, Well Done!")
        score += 1
        time.sleep(1)
    else:
        print("Sorry you got it wrong :(")
        time.sleep(1)
    os.system('clear')

    beginner2 = input("6 + 7 = ")
    if beginner2 == ("13"):
        print("Correct, Well Done!")
        score += 1
        time.sleep(1)
    else:
        print("Sorry you got it wrong :(")
        time.sleep(1)
        os.system('clear')
    
    beginner3 = input("2 + 5 = ")
    if beginner3 == ("7"):
        print("Correct, Well Done!")
        score += 1
        os.system('clear')
        time.sleep(1)
        endquestadditionbeginner()
        print("your score was: ")
        print(score)
        time.sleep(3)
        introduction()
    else:
        print("Sorry you got it wrong :(")
        time.sleep(1)
        os.system('clear')
        endquestadditionbeginner()
        print("your score was: ")
        print(score)
        time.sleep(3)
        introduction()
python math random
1个回答
0
投票

所以这不是你决定采用这个程序的具体方式的答案,但这是一个更简单的方法:

from random import randrange

def beginner_addition():
    A = randrange(1,11)  # Increase range on harder questions
    B = randrange(1,11)  # Ex. for intermediate_addition(), randrange would be (10,21) maybe...
    C = A + B
    ans = input("What's the answer to " + str(A) + "+" + str(B) + "? ")
    if ans == str(C):
        print('Correct')
    else:
        print('Incorrect')

while True:
    beginner_addition()

当然,这只是示例代码。您可以轻松地包含您的积分系统,并且当积分达到一定水平时可能会提升难度。您也可以随机化操作。很抱歉,如果这不是您想要的,但我看到了您的代码,我认为简化您的代码没有任何问题...

© www.soinside.com 2019 - 2024. All rights reserved.