python - 问随机数学问题不太合适

时间:2014-11-11 10:02:48

标签: python math random

我需要帮助我写这个程序。它询问随机数学问题。它在+-x之间进行选择。这是我的代码

import random

def questions():
    name=input("What is your name: ")
    print("Hello there",name,"!")

    choice = random.choice("+-x")
    finish = False
    questionnumber = 0
    correctquestions = 0

    while finish == False:
        if questionnumber < 10 | questionnumber >= 0:
            number1 = random.randrange(1,10)
            number2 = random.randrange(1,10)
            print((number1),(choice),(number2))
            answer=int(input("What is the answer?"))
            questionnumber = questionnumber + 1

            if choice==("+"):
                realanswer = number1+number2
                if answer==realanswer:
                    print("That's the correct answer")
                    correctquestions = correctquestions + 1
                else:
                    print("Wrong answer, the answer was",realanswer,"!")

            if choice==("x"):
                realanswer = number1*number2
                if answer==realanswer:
                    print("That's the correct answer")
                    correctquestions = correctquestions + 1
                else:
                    print("Wrong answer, the answer was",realanswer,"!")

            elif choice==("-"):
                realanswer = number1-number2

                if answer==realanswer:
                    print("That's the correct answer")
                    correctquestions = correctquestions + 1
                else:
                    print("Wrong answer, the answer was",realanswer,"!")
        else:
            finish = True
    else:
            print("Good job",name,"! You have finished the quiz")
            print("You scored " + str(correctquestions) + "/10 questions.")

questions()

输出:

What is your name: s
Hello there s !
6 - 9
What is the answer?-3
That's the correct answer
9 - 8
What is the answer?1
That's the correct answer
9 - 7
What is the answer?2
That's the correct answer
8 - 3
What is the answer?4
Wrong answer, the answer was 5 !
5 - 6
What is the answer?1
Wrong answer, the answer was -1 !
8 - 7
What is the answer?1
That's the correct answer
3 - 5
What is the answer?2
Wrong answer, the answer was -2 !
4 - 5
What is the answer?1
Wrong answer, the answer was -1 !
7 - 2
What is the answer?5
That's the correct answer
7 - 1
What is the answer?6
That's the correct answer
Good job s ! You have finished the quiz
You scored 6/10 questions.

现在程序运行正常,但每次启动程序时,它会使用相同的运算符(+-x来询问问题,但是我遇到了不同的运算符问题但是我想要运行它,以便它实际上在程序中询问不同的添加,减去,乘法问题,以便它询问的所有问题都是不同的问题,例如x+-问题

5 个答案:

答案 0 :(得分:2)

如果你在循环中移动选择部分应该会有所帮助:

while not finish:  # better than finish == False
     choice = random.choice("+-x")
     # etc

答案 1 :(得分:1)

import random

correct = 0
name = input("Please enter your name: ")
for count in range(10):
  num1 = ranom.randint(1, 100)
  num2 = radom.randint(1, 100)
  symbol = rndom.choice(["+", "-", "*"])
  print("Please solve:\n", num1, symbol, num2)
  user = int(input(""))

  if symbol == "+":
    answer = num1 + num2
  elif symbol == "-":
    answer = num1 - num2
  elif symbol == "*":
    answer = num1 * num2

  if user == answer:
    print("Wong u wetard")
    correct = correct + 1
  else:
    print("correct")

print(name, ", You Got", correct, "Out Of 10")

答案 2 :(得分:0)

在完成后== false把这个放! choice = random.choice(“+ - x”)

答案 3 :(得分:0)

我尝试过您遇到的同样问题,在查看您的代码后,我进行了以下详细说明。代码有效,而且(相对)整洁简洁。

def name_enter():
    global name
    name = ""
    while name == "" or len(name) > 25 or not re.match(r'^[A-Za-z0-9-]*$', name):
        name = input("Please enter your name: ")
        enter_class()


def enter_class():  
    global class_choice     
    class_choice = None     
    while class_choice not in ["1","3","2"]:    
        class_choice = input("Please enter you class (1, 2, 3): ") 
    print("\nClass entered was " + class_choice)
    mathsquestion()

def mathsquestion():
    global qa, score
    qa, score = 0, 0
    for qa in range(0,10):
        qa = qa + 1
        print("The question you are currently on is: ", qa)
    n1, n2, userans = random.randrange(12), random.randrange(12), ""
    opu = random.choice(["-","+","x"])
    if opu == "+":
        while userans == "" or not re.match(r'^[0-9,-]*$', userans):
            userans = input("Please solve this: %d" % (n1) + " + %d" % (n2) + " = ") 
        prod = n1 + n2
    elif opu == "-":
        while userans == "" or not re.match(r'^[0-9,-]*$', userans):
            userans = input("Please solve this: %d" % (n1) + " - %d" % (n2) + " = ") 
        prod = n1 - n2
    else:
        while userans == "" or not re.match(r'^[0-9,-]*$', userans):
            userans = input("Please solve this: %d" % (n1) + " x %d" % (n2) + " = ") 
        prod = n1 * n2
    userans = int(userans)
    prod = int(prod)
    if prod == userans:
        score = score + 1
        print("Well done, you have got the question correct. Your score is now: %d" % (score))
    else:
        print("Unfortunatly that is incorrect. The answer you entered was %d" % (userans) + " and the answer is actually %d" % (prod))

    print("Your final score is: %d" % (score))

name_enter()

答案 4 :(得分:0)

这是一个非常不同的代码,但应该做同样的事情。它也更短更整洁。

import random

correct = 0
name = input("Please enter your name: ")
for count in range(10):
    num1 = random.randint(1, 100)
    num2 = random.randint(1, 100)
    symbol = random.choice(["+", "-", "*"])
    print("Please solve:\n", num1, symbol, num2)
    user = int(input(""))

    if symbol == "+":
        answer = num1 + num2
    elif symbol == "-":
        answer = num1 - num2
    elif symbol == "*":
        answer = num1 * num2

    if user == answer:
        print("Correct!")
        correct = correct + 1
    else:
        print("Incorrect")

print(name, ", You Got", correct, "Out Of 10")