2012-01-24 6 views
1

그래서 저는 기본적으로 사람들이 어떤 유형의 질문에 대답하고 얼마나 많은 사람들이 대답하기를 원하는지 그리고 그렇지 않은지에 대해 몇 가지 질문을하는 게임을 만듭니다. 나는 덧셈, 곱셈, 뺄셈과 같은 무작위 수학 연산을하는 법을 알아내는 데 어려움을 겪고있다. 여기 내 모든 코드가 있지만 도움을 구하는 유일한 부분은 세 가지 작업을 혼합하는 방법을 알아야하기 때문에 "혼합"이라고하는 부분입니다.임의 수학 연산

import random 
correct = 0 

while True: 
    questions = int(input("Enter the amount of questions would you like to answer: ")) 
    difficulty = input("Enter the difficulty of questions you would like: Beginner, Intermediate, or Advanced: ") 
math = input("Would you like to do addition, subtraction, multiplication, or mixed: ") 

if difficulty == "Beginner": 
    for i in range(questions): 
     if math == "multiplication": 
      beg1 = random.randint(1, 10) 
      beg2 = random.randint(1, 10) 
      prod = beg1 * beg2 

      begAns = input("What is " + str(beg1) + " times " + str(beg2) + "? ") 

      if int(begAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "subtraction": 
      beg1 = random.randint(1, 10) 
      beg2 = random.randint(1, 10) 
      prod = beg1 - beg2 

      begAns = input("What is " + str(beg1) + " minus " + str(beg2) + "? ") 

      if int(begAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "addition": 
      beg1 = random.randint(1, 10) 
      beg2 = random.randint(1, 10) 
      prod = beg1 + beg2 

      begAns = input("What is " + str(beg1) + " plus " + str(beg2) + "? ") 

      if int(begAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "mixed": 
      beg1 = random.randint(1, 10) 
      beg2 = random.randint(1, 10) 
      prod = beg1 * beg2 

      begAns = input("What is " + str(beg1) + " times " + str(beg2) + "? ") 

      if int(begAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

elif difficulty == "Intermediate": 
    for i in range(questions): 
     if math == "multiplication": 
      intermediate1 = random.randint(1, 25) 
      intermediate2 = random.randint(1, 25) 
      prod = intermediate1 * intermediate2 

      intAns = input("What is " + str(intermediate1) + " times " + str(intermediate2) + "? ") 

      if int(intAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "subtraction": 
      intermediate1 = random.randint(1, 25) 
      intermediate2 = random.randint(1, 25) 
      prod = intermediate1 - intermediate2 

      intAns = input("What is " + str(intermediate1) + " minus " + str(intermediate2) + "? ") 

      if int(intAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "addition": 
      intermediate1 = random.randint(1, 25) 
      intermediate2 = random.randint(1, 25) 
      prod = intermediate1 + intermediate2 

      intAns = input("What is " + str(intermediate1) + " plus " + str(intermediate2) + "? ") 

      if int(intAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "mixed": 
      intermediate1 = random.randint(1, 25) 
      intermediate2 = random.randint(1, 25) 
      prod = intermediate1 + intermediate2 

      intAns = input("What is " + str(intermediate1) + " times " + str(intermediate2) + "? ") 

      if int(intAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

elif difficulty == "Advanced": 
    for i in range(questions): 

     if math == "multiplication": 
      adv1 = random.randint(1, 100) 
      adv2 = random.randint(1, 100) 
      prod = adv1 * adv2 

      advAns = input("What is " + str(adv1) + " times " + str(adv2) + "? ") 

      if int(advAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "subtraction": 
      adv1 = random.randint(1, 100) 
      adv2 = random.randint(1, 100) 
      prod = adv1 - adv2 

      advAns = input("What is " + str(adv1) + " minus " + str(adv2) + "? ") 

      if int(advAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "addition": 
      adv1 = random.randint(1, 100) 
      adv2 = random.randint(1, 100) 
      prod = adv1 + adv2 

      advAns = input("What is " + str(adv1) + " plus " + str(adv2) + "? ") 

      if int(advAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

     elif math == "mixed": 
      adv1 = random.randint(1, 100) 
      adv2 = random.randint(1, 100) 
      prod = adv1 + adv2 

      advAns = input("What is " + str(adv1) + " times " + str(adv2) + "? ") 

      if int(advAns) == prod: 
       print("That's right -- well done.\n") 
       correct += 1 
      else: 
       print("No, I'm afraid the answer is ",prod) 

else: 
    print("Please enter Beginner, Intermediate, or Advanced.\n") 


print("\nI asked you", questions, "questions. You got ", correct, " of them right.") 

if correct/questions > 2/3: 
    print("Well done.\n") 
elif correct/questions > 1/3: 
    print("You need more practice.\n") 
else: 
    print("Please ask your math teacher for help!\n") 

restart = input("Would you like to play again? Y/N: ") 
if restart == "Y": 
    continue 
elif restart == "N": 
    break 
else: 
    print("Please Enter Y or N") 
+1

, 당신은 당신이 원하는 특정 부분에 코드를 깎다하기 위해 열심히 노력한다 도와주세요. 나는 downvote하지 않았다. 그러나 그것은 아마도 다른 누군가가 한 이유 일 것이다. –

+0

코드가 두 가지 기능을 사용하면 이익을 얻을 수 있습니다. 한 곳에서 너무 많은 일을 할 수 있습니다. –

답변

7

무작위, + 중 하나를 선택 - 또는 *와 두 개의 숫자에 적용 : 나중에 참조 할 수 있도록

import random 
from operator import add, sub, mul 

ops = (add, sub, mul) 
op = random.choice(ops) 

beg1, beg2 = random.randint(1,10), random.randint(1,10) 

ans = op(beg1, beg2)