Python 不知道为什么循环不中断
所以我错过了一些东西。当用户已用完其所有的积分,并被提示是否要向池中添加更多的积分,并且用户选择“n”时,系统将持续提示用户向池中添加更多的积分。这是不应该发生的。我试着摆弄一下休息的位置,但没有用。当用户输入“n”时,整个循环应停止。任何帮助都将不胜感激。谢谢Python 不知道为什么循环不中断,python,python-3.x,while-loop,break,Python,Python 3.x,While Loop,Break,所以我错过了一些东西。当用户已用完其所有的积分,并被提示是否要向池中添加更多的积分,并且用户选择“n”时,系统将持续提示用户向池中添加更多的积分。这是不应该发生的。我试着摆弄一下休息的位置,但没有用。当用户输入“n”时,整个循环应停止。任何帮助都将不胜感激。谢谢 # Char Creator- pool of 30 pts, spend on Str, Health, Wisdom, Dex # Spend points in pool on attr, take points and put
# Char Creator- pool of 30 pts, spend on Str, Health, Wisdom, Dex
# Spend points in pool on attr, take points and put them back into pool
pool = 30
attr = {'Strength' : 0, 'Health' : 0, 'Wisdom' : 0, 'Dexterity' : 0}
while True:
while pool > 0:
print('\t\t Time to spend some points! You have', pool, 'to spend')
spend = input('Where would you like to spend your points?')
if spend in attr:
amount = int(input('How many points would you like to spend?'))
if amount > pool:
print('You do not have that many points! You have', pool, 'points!')
else:
attr[spend] += amount
pool -= amount
print(attr)
else:
print('Not a valid input. Please correct.')
print('\t\t You have no more points! Add some to the pool!')
while pool == 0:
confirm = input('Would you like to add points back into the pool?')
if confirm == 'y':
take = input('Where would you like to take points from?')
if take in attr:
number = int(input('How many points would you like to take?'))
if attr[take] >= number:
pool += number
attr[take] -= number
print ('There are now', pool, 'points remaining')
elif attr[take] < number:
print('You dont have enough points to do that! You have', attr[take], 'points in', take)
else:
print('Please make a valid selection.')
elif confirm == 'n':
print('Goodbye!')
break
#Char Creator-共30点,用于Str、健康、智慧和Dex
#在attr上花费池中的点数,获取点数并将其放回池中
游泳池=30
attr={'Strength':0,'Health':0,'willity':0,'dextrity':0}
尽管如此:
当池>0时:
打印('\t\t花点时间!你有',池',花')
花费=输入('您想在哪里花费您的积分?')
如果在attr中花费:
amount=int(输入('您希望花费多少积分?'))
如果金额>池:
打印('您没有那么多积分!您有',池',积分!')
其他:
属性[支出]+=金额
池-=金额
打印(属性)
其他:
打印('不是有效输入。请更正')
打印('\t\t您没有更多的点数!请向池中添加一些!')
当池==0时:
confirm=input('是否要将点添加回池?')
如果确认==“y”:
take=input('您想从哪里取点?')
如果接收属性:
number=int(输入('您想取多少分?'))
如果attr[take]>=编号:
池+=数量
attr[take]-=编号
打印('现在有',池,'剩余点数')
elif attr[take]<编号:
打印('你没有足够的分数来做这件事!你有',attr[take],'points in',take)
其他:
打印('请进行有效选择')
elif confirm==“n”:
打印('再见!')
打破
您的中断
未缩进到elif
语句中(或内部while循环中):
在break
前面再添加两个选项卡
为了使您的程序能够在中断内部循环后结束,您可能还需要进行更改
while True:
到
谢谢你的回答——显然这是一个愚蠢的问题。我只是个初学者。我自己也尝试过,但出于某种原因,仍会提示用户向池中添加更多的点。@Zack:这是因为您的
break
语句仅中断内部while循环,而不是外部while循环。将while True:
行更改为while points>0
,您将修复该问题。您还必须在打印后添加中断
行('现在有',池,'剩余点')
,以便退出该循环。看起来您的一些while
循环应该是if
语句或omitted@cmd:它们是而由于打印而循环(“无效输入。请更正”)
或打印(“请进行有效选择”)
路径
while True:
while points > 0: