Python 如何正确使用while循环和None函数?
大家好,我一直在尝试制作这个程序,这样他们就不会有小故障或作弊了。我只是想不出我们如何在第[]行使用while循环,这样,如果没有输入任何内容,程序就不会继续运行Python 如何正确使用while循环和None函数?,python,loops,for-loop,python-3.x,while-loop,Python,Loops,For Loop,Python 3.x,While Loop,大家好,我一直在尝试制作这个程序,这样他们就不会有小故障或作弊了。我只是想不出我们如何在第[]行使用while循环,这样,如果没有输入任何内容,程序就不会继续运行 #Running pertty good but needs work! #charector creator program v3 #values def getString(prompt): result = input(prompt) while len(result) == 0: result
#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行,我确实需要intinputyadayad,因为如果我没有int输入,它会给我一个int>str cannot do或类似的奇怪错误
请帮忙!
另外,我对编程相当陌生,所以你能在某种程度上使用代码并将其翻译成外行术语吗?谢谢:错误消息非常清楚地说明了问题:空输入不能转换为int。一般来说,你不能假设用户只输入一个数字字符串,所以你需要设法处理 常见的习惯用法是使用异常处理程序进行包装:
while True:
try:
add=int(input("How many points? "))
break
except ValueError:
pass
您在第49行输入了什么?