Python 数学方程生成程序

Python 数学方程生成程序,python,random,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 la

我一直在创建这个数学方程生成器程序,它会根据你的难度向你提问。我现在正在运行它,但是当程序问你想要什么困难时,它会不断重复,我不知道如何解决它。我只是想知道我能做些什么来解决这个问题

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()
您可以使用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: "))
不清楚用户应该输入什么来响应此提示。看起来您希望输入一个整数,但是提示提示应该输入一个字符串初学者、中级或高级。代码的其余部分也需要这些字符串中的一个,因此您应该将输入语句更改为:

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: ")

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

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')

使用难度等级={‘初学者’、‘中级’、‘高级’}并将其设置为一组可能会稍微好一些。我现在已经整理好了难度等级,但当你进入初学者、中级或高级课程时,它不会问任何问题。你知道它为什么这么做吗?你如何解决它?没有看到修改后的代码,不。你应该问另一个问题,但尽量减少你在该问题中发布的代码量;将其设置为演示问题所需的最小代码量。虽然这将避免无限循环,但它也允许将难度变量设置为整数值,这与代码的其余部分不兼容——我认为这一细节至少值得指出,尽管OP没有提到这个问题,而且还有很多其他问题。就我的2₤。考虑到困难=int。。。很难忽略的是,难度将设置为int,因此永远不等于字符串-当发布的代码有37个bug时,一个答案中应该指出多少个bug?-当OP挑出it不断重复的问题时,我选择专门解决37个bug中的一个…:-