如何正确使用while循环和None函数?

2024-05-13 14:35:53 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我一直在努力使这个程序,使他们没有小故障或作弊。我只是不知道如何在第[]行使用while循环,这样如果没有输入任何内容,程序就不会继续运行。在

#Running pertty good but needs work!
#charector creator program v3
#values
def getString(prompt):
    result = input(prompt)
    while len(result) == 0:
        result = input(prompt)
    return result

def Character_Creator_Function():
    NAME = getString("Please enter your name: ")
    if (input("Would you like instructions?(y,n): ") in ['y','Y']):
        printInstructions()
    print("Welcome",NAME)
    stats=[NAME, "HP", "STR", "AC", "DAM" "LCK", "DEF"]
    HP=0
    STR=0
    AC=0
    DAM=0
    LCK=0
    DEF=0
    #interface begins
    op=31
    points=30
    choice=None
    print("Welcome to the Charector Creater Program!")
    while choice!="0":
        print("\nYou have",points,"points to spend on various stats!")
        #list below
        print(
            """
            1= Add points
            2= Take away points
            3= View Stats
            4= Begin Battle!
            """)
        choice=input("choice:")

        #choice 1
        if choice=="1":
            stats=input("""
            1- Health Points
            2- Strenght Points
            3- Armor Class Points
            4- Damage Points
            5- Luck Points
            6- defense Points
            """)
            add=int(input("how many points?"))
            if add<=points and add>0:
                if stats=="1":
                    HP+=add
                    print( HP, "= Health points.")
                elif stats=="2":
                    STR+=add
                    print( STR, "= Strenght points.")
                elif stats=="3":
                    AC+=add
                    print( AC, "= Aromor Class points.")
                elif stats=="4":
                    DAM+=add
                    print( DAM, "= Damage points.")    
                elif stats=="5":
                    LCK+=add
                    print( LCK, "= Luck points.")
                elif stats=="6":
                    DEF+=add
                    print( DEF, "= defense points.")
                points-=add
                print("you have", points)

            else:
                     print("""
                               Invalid answer!
                   You either do not have enought points.
                                     OR
                     You are entering a negitive number!
                     """)

    #choice 2
        if choice=="2":
            stats=input("""
            1- Health Points
            2- Strenght Points
            3- Armor Class Points
            4- Damage Points
            5- Luck Points
            6- defense Points
            """)
            take=input("how many points?")
            while take==0:
                if stats=="1" and take<=HP:
                    HP-=take #HP
                    print( HP, "= Health points.")
                elif stats=="2" and take<=STR:
                    STR-=take #STR
                    print( STR, "= Strenght points.")
                elif stats=="3" and take<=AC:
                    AC-=take #AC
                    print( AC, "= Aromor Class points.")
                elif stats=="4"and take<=DAM:
                    DAM-=take #DAM
                    print( DAM, "= Damage points.")    
                elif stats=="5"and take<=LCK:
                    LCK-=take #LCK
                    print( LCK, "= Luck points.")
                elif stats=="6"and take<=DEF:
                    DEF-=take #DEF
                    print( DEF, "= Defense points.")
                points+=take
            else:
                print("""
                               Invalid answer!
                    You are do not have that many points!
                               """)

       #choice 3
        if choice=="3":
             print("Health:", HP)
             print("Strenght:", STR)
             print("Armor Class:", AC)
             print("Damage:", DAM)
             print("Luck:", LCK)
             print("Defense:", DEF)

        #choice 4
        if choice=="4":
            print("You have Compleated your Charector creator program! Congradulations! you are now ready to begin battle! HuRa!")
            print("HP:",HP,"\nSTR:",STR,"\nAC:",AC,"\nDAM:",DAM,"\nLCK:",LCK,"\nDEF:",DEF ,"\n")
            return [NAME,HP,STR,AC,DAM,LCK,DEF]

        #for programmers use to navagate through out the program with ease
        if choice=="x":

            HP=1000
            STR=10
            AC=5
            DAM=10
            LCK=100
            DEF=5
            print("HP:",HP,"\nSTR:",STR,"\nAC:",AC,"\nDAM:",DAM,"\nLCK:",LCK,"\nDEF:",DEF ,"\n")
            print("You have Compleated your Charector creator program! Congradulations!")
            print("You are now ready to begin battle! HuRa!\n\n")
            return [NAME,HP,STR,AC,DAM,LCK,DEF]
Character_Creator_Function()

这是我得到的错误:

^{pr2}$

在第49行,我确实需要int(input("yadayadyad"),因为如果我没有int输入,它会给我一个int>;str不能做的或类似的奇怪错误。在

请帮忙!
另外,我对编程还很陌生,所以你能在一定程度上使用代码,而翻译成外行的术语,可以说,谢谢:)


Tags: addinputdefstatsacpointshpprint
1条回答
网友
1楼 · 发布于 2024-05-13 14:35:53

错误消息非常清楚地说明了问题:空输入不能转换为int。一般来说,您不能假设用户只输入一个数字字符串,所以您需要以某种方式处理。在

一个常见的习惯用法是用异常处理程序包装:

while True:
    try:
        add=int(input("How many points? "))
        break
    except ValueError:
        pass

相关问题 更多 >