大家好,我一直在尝试制作这个节目,因此它们不是故障或欺骗。我只是无法想象如何在行[]上使用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()
这是我得到的错误:
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")
,因为如果我没有int输入,它会给我一个int&gt; str不能做或者像那样奇怪的错误。
请帮助!
PS。我对编程很陌生,所以你可以在一定程度上使用代码,并且翻译成非专业人士的条款可以说,谢谢:)
答案 0 :(得分:0)
错误消息非常明确地说明了问题:空输入无法转换为int
。一般来说,你不能假设用户只输入一个数字字符串,所以你需要以某种方式处理。
一个常见的习惯用语是用异常处理程序包装:
while True:
try:
add=int(input("How many points? "))
break
except ValueError:
pass