寻找编码项目的建议

时间:2017-07-01 07:10:00

标签: python

因此,如果给出的答案太高或太低,代码需要重新询问给出的问题。此外,如果答案是正确的,它应该告诉他们然后循环回到问题('你想要多少问题?')

def main():

    gamenumber = int(input("How many problems do you want?\n"))
    count = 0
    while count < gamenumber:
        num_1 = randint(1,10)
        num_2 = randint(1,10)
        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
        answer = (num_1*num_2)
        count += 1

        for guess in range(1):
            if guess == answer:
                print (' Thats Correct!')
        for guess in range(1):
            if guess > answer:
                print (' Answer is to high')
        for guess in range(1):
            if guess < answer:
                print ('Answer is to low')
main()

2 个答案:

答案 0 :(得分:1)

首先,请检查您的代码。您在for循环中使用了“guess”变量。执行程序时,猜测值为40(4X10)。当执行for语句时,猜测值变为0,因为您将输出设置为低。确保将用于循环的变量更改为“num”,然后检查输出。

为什么使用3 for循环,你可以在单个for循环中执行此操作。

请找到以下代码: -

from random import randint
def main():

    ans = 'y'
    while ans != 'n':
        gamenumber = int(input("How many problems do you want?\n"))
        count = 0
        while count < gamenumber:
            num_1 = randint(1,10)
            num_2 = randint(1,10)
            guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
            print(guess)
            answer = (num_1*num_2)
            print("=====>",answer)
            count += 1

            for num in range(1):
                if guess == answer:
                    print (' Thats Correct!')
                elif guess > answer:
                    print (' Answer is to high')
                elif guess < answer:
                    print ('Answer is to low')
        yes_no_input = str(input("Do you want to continue (y/n) ?"))
        ans = accept_ans(yes_no_input)
        if ans == 'n':
            print("thanks for the test")
            break;

def accept_ans(ans):

    if not ans.isalpha():
        print("Enter only y and n")
        ans = str(input("Do you want to continue (y/n) ?"))

    if ans == 'y' or ans == 'n':
        return ans

    if ans != 'y' or ans != 'n':
        print("please enter y for YES and n for NO")
        ans = str(input("Do you want to continue (y/n) ?"))
        if ans != 'y' or ans != 'n':
            accept_ans(ans)

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

之后

print("Thats correct")

你需要打电话给

main()

再次发挥作用。