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

1 投票
1 回答
614 浏览
提问于 2025-04-18 04:38

大家好,我一直在尝试制作这个程序,希望它没有任何漏洞或作弊的可能。可是我就是搞不懂怎么在第[]行使用一个循环,这样如果没有输入内容,程序就不会继续运行。

#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()

这是我遇到的错误:

Traceback (most recent call last):
  File "/Users/alexmasse/Desktop/ExampleQuest final/Charector Creator 100% functions, NOT IDEAL.py", line 145, in <module>
    Character_Creator_Function()
  File "/Users/alexmasse/Desktop/ExampleQuest final/Charector Creator 100% functions, NOT IDEAL.py", line 49, in Character_Creator_Function
    add=int(input("how many points?"))
ValueError: invalid literal for int() with base 10: ''

在第49行,我确实需要用到int(input("yadayadyad"),因为如果我不使用这个整数输入,就会出现一个错误,提示说整数和字符串不能一起用,或者类似的奇怪错误。

请帮帮我!
顺便说一下,我对编程还比较陌生,所以能不能尽量用简单的代码,并把它翻译成通俗易懂的话,谢谢 :)

1 个回答

0

错误信息很清楚地说明了问题:空输入无法转换为一个整数(int)。一般来说,你不能指望用户只输入数字字符串,所以你需要想办法处理这种情况。

一个常见的做法是使用异常处理来包裹代码:

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

撰写回答