Python数学游戏级别没有变化

时间:2017-12-03 19:12:38

标签: python

当我输入level4level3level2时,我的游戏未达到要求的级别。

它打印输入的等级(例如level1)并保持在第一级。

print("Welcome to the maths skills testing machine!")
userinput = input("\nAre you ready to test your maths skills?")

print("\nWhich level would you like to play?")
userinput = input('''Level 1, Level 2, Level 3, Level 4''')

if userinput=="level1":
    level1()
elif userinput=="level2":
    level2()
elif userinput=="level3":
    level3()
elif userinput=="level4":
    level4()

def level1():

    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(1, 10)
    num_b = random.randint(1, 10)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    while user_answer == expected_answer:
        level1()
    else:
        exit()

level1()


def level2():

    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(10, 50)
    num_b = random.randint(10, 50)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    while user_answer == expected_answer:
        level2()
    else:
        exit()

level2()


def level3():

    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(50, 100)
    num_b = random.randint(50, 100)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    while user_answer == expected_answer:
        level3()
    else:
        exit()

level3()


def level4():

    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(100, 1000)
    num_b = random.randint(100, 1000)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the 
question'.format(time.time() - start))
    while user_answer == expected_answer:
        level4()
    else:
        exit()

level4()

2 个答案:

答案 0 :(得分:0)

在if语句中,您使用的是input()方法。它不一定在那里。只需写下:

if userinput=="level1":
    level1()
elif userinput=="level2":
    level2()

等。最好使用elif代替if

答案 1 :(得分:0)

您应该在使用之前定义该功能。您在定义函数之前调用函数。

也不需要在它们之后调用它们(例如level1())。 您也可以在文件的乞讨处移动导入,并从每个函数中删除它们。 您也可以使用IDE,例如PyCharm

def level1():
    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(1, 10)
    num_b = random.randint(1, 10)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    while user_answer == expected_answer:
        level1()
    else:
        exit()


def level2():
    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(10, 50)
    num_b = random.randint(10, 50)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    while user_answer == expected_answer:
        level2()
    else:
        exit()


def level3():
    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(50, 100)
    num_b = random.randint(50, 100)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    while user_answer == expected_answer:
        level3()
    else:
        exit()


def level4():
    import operator
    import random
    import time
    start = time.time()

    ops = {'+': operator.add,
           '-': operator.sub}

    num_a = random.randint(100, 1000)
    num_b = random.randint(100, 1000)
    op = random.choice(tuple(ops.keys()))

    print('{}{}{}=?'.format(num_a, op, num_b))
    expected_answer = ops[op](num_a, num_b)
    user_answer = int(input())
    if user_answer == expected_answer:
        print('Correct')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    else:
        print('Wrong')
        print('It took you {0:0.1f} seconds to answer the question'.format(time.time() - start))
    while user_answer == expected_answer:
        level4()
    else:
        exit()


print("Welcome to the maths skills testing machine!")
_ = input("\nAre you ready to test your maths skills?")

print("\nWhich level would you like to play?")
userinput = input('''Level 1, Level 2, Level 3, Level 4''')

if userinput=="level1":
    level1()
elif userinput=="level2":
    level2()
elif userinput=="level3":
    level3()
elif userinput=="level4":
    level4()