程序无故退出python

时间:2018-06-20 18:39:43

标签: python-3.x

所以我已经输入了这个单词,并且已经阅读了几个小时,而且我似乎无法理解它的问题所在。当我运行它并选择一个选项时,它就退出了。我已经使用IDLE来运行它,并且为了运行功能dice()或guess(),我必须手动进行操作。预先感谢

from random import randint

def main ():
    print ("Choose a game.")
    print ("1. Dice")
    print ("2. Guess")
    choice = input ("Enter your choice: ")

    if choice == 1:
        dice ()
    elif choice == 2:
        guess ()
    elif choice == 'Q' or choice == 'q':
        exit ()


def dice ():
    rolling = True
    while rolling:
        n = input ("How many dice? ")
        if n == 'M' or n == 'm':
            main ()
        elif n == 'Q' or n == 'q':
            exit ()
        else:
            for i in range (int (n)):
                print (randint (1, 6))


def guess ():
    guessing = True
    while guessing:
        print ("Guess the number: 1-1000")
        n = randint (1, 1000)
        count = 0
        wrong = True
        while wrong:
            g = input ("Guess: ")
            if g == 'M' or g == 'm':
                main ()
            elif g == 'Q' or g == 'q':
                exit ()
            else:
                if int (g) > n:
                    print ("Lower")
                    count += 1
                elif int (g) < n:
                    print ("Higher")
                    count += 1
                else:
                    print ("Correct")
                    count += 1
                    wrong = False
                    print ("You took " + str (count) + " guesses")


main ()

2 个答案:

答案 0 :(得分:0)

如注释中所指出的,引起问题的原因是delete返回一个字符串,但是您正在检查是否等于整数。以下代码应该起作用:

input

答案 1 :(得分:0)

我修正了我的评论中指出的比较错误。优化了程序流程,因此您不会像其他人所指出的那样深入了解函数调用堆栈:

from random import choices, randint

def quit():
    exit(0)     

def myInput(text):
    """Uses input() with text, returns an int if possible, uppercase trimmed string else."""
    p = input(text).strip().upper()
    try:
        p = int(p)
    except ValueError:
        pass

    return p

def dice ():
    while True:
        n = myInput ("How many dice? ")
        if n == 'M':
            break  # back to main() function
        elif n == 'Q':
            quit() # quit the game
        elif isinstance(n,int):  # got an int, can work with that
            # get all ints in one call, print decomposed list with sep of newline
            print (*choices(range(1,7), k = n), sep="\n")
        else: # got no int, so error input
            print("Input incorrect: Q,M or a number accepted.")


def guess ():
    while True:
        print ("Guess the number: 1-1000")
        n = randint (1, 1000)
        count = 0
        while True:
            g = myInput ("Guess: ")
            if g == 'M':
                return  # back to main menue
            elif g == 'Q':
                quit () # quit the game
            elif isinstance(g,int): # got a number, can work with it
                count += 1
                if g != n:  # coditional output, either lower or higher
                    print ("Lower" if g < n else "Higher")
                else:
                    print ("Correct")
                    print ("You took {} guesses".format(count))
            else: # not a number, incorrect
                print("Input incorrect: Q,M or a number accepted.")



# which function to call on which input
options = { "1" : dice, "2" : guess, "Q":quit }

def main ():
    while True:
        print ("Choose a game.")
        print ("1. Dice")
        print ("2. Guess")
        choice = input ("Enter your choice: ").upper()

        if choice in options:
            options[choice]()  # call the function from the options-dict
        else:
            print("Wrong input.")

main ()