python 为什么进程停止了,即使我有一些命令?[重复]

enyaitl3  于 2023-04-04  发布在  Python
关注(0)|答案(2)|浏览(123)

此问题在此处已有答案

How can I read inputs as numbers?(10个答案)
两年前关闭了。
我正在尝试做一个代码猜谜游戏,用户可以选择代码的范围,用户尝试猜随机生成的代码,直到猜对为止,计算机还会显示用户猜对了哪些数字,问题是当用户确实猜对了代码这个过程只是停止,即使我的代码说打印一个祝贺消息,并再次播放功能。请任何人都可以帮助?谢谢。代码:

import random
import string

def get_range():
    Min = input("ENTER THE MINIMUM NUMBER THE CODE CAN BE: ")
    Max = input("ENTER THE MAXIMUM NUMBER THE CODE CAN BE: ")

    validate_range(Min, Max)

def validate_range(Min, Max):
    Check_Min = Min.isdigit()
    Check_Max = Max.isdigit()

    if Check_Min is not True or Check_Max is not True:
        print("INPUT MUST ONLY INCLUDE INTEGERS! ")
        get_range()
    elif Min == Max:
        print("MINIMUM AND MAXIMUM NUMBER MUST NOT BE EQUIVALENT! ")
        get_range()
    elif Min > Max:
        print("MINIMUM NUMBER MUST NOT BE GREATER THAN MAXIMUM NUMBER!")
        get_range()
    else:
        Random = random.randrange(int(Min), int(Max))
        get_guess(Random)

def get_guess(Random):
    Guess = str(input("ENTER YOUR GUESS: "))
    Check_Guess = Guess.isdigit()

    if not Check_Guess:
        print("INPUT MUST ONLY CONTAIN INTEGERS! ")
        get_guess(Random)
    else:
        validate_guess(Guess, Random)

def validate_guess(Guess, Random):
    Length = len(str(Random))
    Digits_Correct = 0

    if Guess == Random:
        print("WELL DONE! YOU GUESSED THE NUMBER! ")
        play_again()
    else:
        Digits = ["?"] * Length

        for i in range(0, int(Length)):
            if str(Guess)[i] == str(Random)[i]:
                Digits[i] = Guess[i]
                Digits_Correct += 1
            else:
                continue

        if int(Length) > Digits_Correct > 0:
            print("NOT QUITE! YOU GOT", Digits_Correct, " DIGITS CORRECT!")
            print(Digits)
            get_guess(Random)
        elif Digits_Correct == 0:
            print("NONE OF YOUR DIGITS MATCH! ")
            get_guess(Random)

def play_again():
    Choice = input("\n DO YOU WISH TO PLAY AGAIN? (Y/N)")

    if Choice != "Y" or Choice != "N" or Choice != "y" or Choice != "n":
        print("PLEASE ENTER A VALID INPUT! ")
        play_again()
    else:
        get_range()

print("WELCOME TO CODE CRUNCHERS!\n ")
get_range()
vcirk6k6

vcirk6k61#

我认为这里的问题是你的Guess是一个字符串,而你的Random是一个整数。要解决这个问题,你可以尝试将Guess转换为整数或将Random转换为字符串。试试这个:

def validate_guess(Guess, Random):
    Length = len(str(Random))
    Digits_Correct = 0

    if int(Guess) == Random:
        print("WELL DONE! YOU GUESSED THE NUMBER! ")
        play_again()
h6my8fg2

h6my8fg22#

我认为你的问题是strint类型。首先,你的MinMax是字符串,所以你的行:elif Min > Max: print("MINIMUM NUMBER MUST NOT BE GREATER THAN MAXIMUM NUMBER!")不能正常工作。另一个问题是你的变量GuessRandom是不同的类型,所以Guess == Random总是会返回False
下面是正确的代码版本。
我还添加了一些if案例,以便能够在不关闭程序的情况下退出程序。

import random
import string

def get_range():
    Min = input("ENTER THE MINIMUM NUMBER THE CODE CAN BE: ")
    Max = input("ENTER THE MAXIMUM NUMBER THE CODE CAN BE: ")
    if Min == 'q':
        return

    validate_range(Min, Max)

def validate_range(Min, Max):
    Check_Min = Min.isdigit()
    Check_Max = Max.isdigit()

    if Check_Min is not True or Check_Max is not True:
        print("INPUT MUST ONLY INCLUDE INTEGERS! ")
        get_range()

    Min = int(Min)
    Max = int(Max)

    if Min == Max:
        print("MINIMUM AND MAXIMUM NUMBER MUST NOT BE EQUIVALENT! ")
        get_range()
    elif Min > Max:
        print("MINIMUM NUMBER MUST NOT BE GREATER THAN MAXIMUM NUMBER!")
        get_range()
    else:
        Random = random.randrange(int(Min), int(Max))
        get_guess(Random)

def get_guess(Random):
    Guess = str(input("ENTER YOUR GUESS: "))
    Check_Guess = Guess.isdigit()

    if not Check_Guess:
        print("INPUT MUST ONLY CONTAIN INTEGERS! ")
        get_guess(Random)
    else:
        validate_guess(Guess, Random)

def validate_guess(Guess, Random):
    Length = len(str(Random))
    Digits_Correct = 0

    if int(Guess) == Random:
        print("WELL DONE! YOU GUESSED THE NUMBER! ")
        play_again()
    else:
        Digits = ["?"] * Length

        for i in range(0, int(Length)):
            if str(Guess)[i] == str(Random)[i]:
                Digits[i] = Guess[i]
                Digits_Correct += 1
            else:
                continue

        if int(Length) > Digits_Correct > 0:
            print("NOT QUITE! YOU GOT", Digits_Correct, " DIGITS CORRECT!")
            print(Digits)
            get_guess(Random)
        elif Digits_Correct == 0:
            print("NONE OF YOUR DIGITS MATCH! ")
            get_guess(Random)

def play_again():
    print("\n DO YOU WISH TO PLAY AGAIN? (Y/N)")
    Choice = input()

    if Choice != "Y" or Choice != "N" or Choice != "y" or Choice != "n":
        print("PLEASE ENTER A VALID INPUT! ")
        play_again()
    else:
        get_range()

print("WELCOME TO CODE CRUNCHERS!\n ")
get_range()

相关问题