数学方程生成器程序

时间:2015-03-16 04:02:56

标签: python random

我一直在创建这个数学方程式生成器程序,根据你的难度提出问题。我现在已经开始运行,但是当程序问你想要什么困难时,它一直在重复它,我不知道如何解决它。我只是想了解一下如何解决它。

import random
import time
import sys


correct = 0  #Amount the user has gotten correct
ans = 0 #Holds the answer to the question
lastName = str()   #holds last name
firstName = str()   #holds first name
className = str()    #holds user's form
difficulty = int() #where user input what diffliculty they want
Beginner = 0 
beginner = 0
Intermediate = 0
intermediate = 0
Advanced = 0
advanced = 0

F = firstName = raw_input("Please enter your first name: ").title()
L = lastName = raw_input("Please enter your surname: ").title()
C = className = raw_input("Please enter your form class: ").title()
print "Hi", F, L, 
print ''

time.sleep(1)

while True:
    difficulty = int(input("Please select a difficulty level: Beginner, Intermediate, or Advanced: "))

if difficulty == "Beginner":
    def multiplication(): #Creates a multiplication question in beginner 
        global ans
        numOne, numTwo = random.randint(0,10), random.randint(0,10)
        print "What is", numOne, "*" , numTwo, "?"
        ans = (numOne*numTwo)

    def addition(): #Creates a addition question in beginner 
        global ans
        numOne, numTwo = random.randint(0,10), random.randint(0,10)
        print "What is", numOne, "+" , numTwo, "?"
        ans = (numOne+numTwo)

    def subtraction(): #Creates a subtraction question in beginner 
        global ans
        numOne, numTwo = random.randint(0,10), random.randint(0,10)
        print "What is", numOne, "-" , numTwo, "?"
        ans = (numOne-numTwo)

operation = [multiplication,subtraction,addition]   #holds all of the opperators
randOperation = random.choice(operation)    #chooses a random operator

if difficulty == "Intermediate":
    def multiplication(): #Creates a multiplication question in intermediate 
        global ans
        numOne, numTwo = random.randint(10,20), random.randint(10,20)
        print "What is", numOne, "*" , numTwo, "?"
        ans = (numOne*numTwo)

    def addition(): #Creates a addition question in intermediate
        global ans
        numOne, numTwo = random.randint(10,20), random.randint(10,20)
        print "What is", numOne, "+" , numTwo, "?"
        ans = (numOne+numTwo)

    def subtraction():#Creates a subtraction question in intermediate 
        global ans
        numOne, numTwo = random.randint(10,20), random.randint(10,20)
        print "What is", numOne, "-" , numTwo, "?"
        ans = (numOne-numTwo)

operation = [multiplication,subtraction,addition]   #holds all of the opperators
randOperation = random.choice(operation)    #chooses a random operator

if difficulty == "Advanced":
    def multiplication(): #Creates a multiplication question in advanced 
        global ans
        numOne, numTwo = random.randint(20,35), random.randint(20,35)
        print "What is", numOne, "*" , numTwo, "?"
        ans = (numOne*numTwo)

    def addition(): #Creates a addition in advanced  
        global ans
        numOne, numTwo = random.randint(20,35), random.randint(20,35)
        print "What is", numOne, "+" , numTwo, "?"
        ans = (numOne+numTwo)

    def subtraction(): #Creates a subtraction question in advanced
        global ans
        numOne, numTwo = random.randint(20,35), random.randint(20,35)
        print "What is", numOne, "-" , numTwo, "?"
        ans = (numOne-numTwo)

operation = [multiplication,subtraction,addition]   #holds all of the opperators
randOperation = random.choice(operation)    #chooses a random operator


def main():   #main game loop - ask questions and checks it against answer, stops are a give amount of questions
    question = 0
    user_score = 0
    randOperation = random.choice(operation)

    while True:
        try:
            randOperation()
            randOperation = random.choice(operation)
            if question >= 12:
                break
            userInput = int(input("Enter the answer: "))
            if userInput == ans:
                print("Correct!" + "\n")
                user_score += 1
                question += 1
            else:
                print("Incorrect!" + "\n")
                question += 1
        except ValueError:
            print("I'm sorry that's invalid")
            question += 1

main()    #initializes the function

print(firstName, lastName , "you scored" , user_score , "out of 10")   #shows the user's score and name

user_name = firstName + ' ' + lastName
function(user_score,user_name)


def endMenu():
    while True:
        try:  
            options = int(input('''Press '1' to view users' scores,
            press '2' to restart the test,
            press '3' to exit the game,

            Enter option here: '''))
        except ValueError:
            print("I'm sorry that was invalid...")

        if options == 3:  #exits the game...
            sys.exit()

        elif options == 2:   #starts the game loop again because it's in a function
            main()

        elif options == 1:   #displays everything on the .txt file
            f = open('userScore.txt', 'r')
            print(f.read())
            print()
            endMenu()

        else:
            print("Sorry, I don't understand. Please try again...")
            print()
            endMenu()        

endMenu()

2 个答案:

答案 0 :(得分:2)

你有input()语句来获得无限循环中的难度级别:

while True:
    difficulty = int(input("Please select a difficulty level: Beginner, Intermediate, or Advanced: "))

摆脱循环:

difficulty = int(input("Please select a difficulty level: Beginner, Intermediate, or Advanced: "))

目前还不清楚用户应该在响应此提示时输入什么内容。看起来你想要输入一个整数,但提示符表示其中一个字符串" Beginner"," Intermediate"或者" Advanced"应该输入。其余代码也需要其中一个字符串,因此您应将input()语句更改为:

difficulty = raw_input("Please select a difficulty level: Beginner, Intermediate, or Advanced: ")

如果要在继续之前验证用户输入,可以像这样循环:

difficulty_levels = ['Beginner', 'Intermediate', 'Advanced']
difficulty = None
while difficulty not in difficulty_levels:
    difficulty = raw_input("Please select a difficulty level: Beginner, Intermediate, or Advanced: ")

答案 1 :(得分:1)

这个循环永远不会结束......:

while True:
    difficulty = int(input("Please select a difficulty level: Beginner, Intermediate, or Advanced: "))

据推测,你正在寻找类似的东西:

while True:
    try:
        difficulty = int(input("Please select a difficulty level: Beginner, Intermediate, or Advanced: "))
    except ValueError:
        pass
    else:
        if 0 <= difficulty <= 2: break
    print('Please enter 0, 1, or 2')