Python 不可靠的嵌套while循环?
我在这里有一个逻辑混乱,不知道我的设置是否有可能解决 我试图提示用户(按顺序)Python 不可靠的嵌套while循环?,python,while-loop,logic,Python,While Loop,Logic,我在这里有一个逻辑混乱,不知道我的设置是否有可能解决 我试图提示用户(按顺序) 用户回答y/n(最初设置为“y”) 打赌(基于他们当前的钱) 对数字1-6的猜测 在用户回答除y以外的任何问题之前,我将循环此程序。 在第2阶段,如果赌注无效/不在他们当前资金的范围内,我将循环要求下注。 在第3阶段,我将循环询问猜测是否为1-6或无效 如果用户始终使用有效的猜测回答,则我的以下代码有效: def roll(): return [random.randrange(1,6), random.ra
y
以外的任何问题之前,我将循环此程序。
在第2阶段,如果赌注无效/不在他们当前资金的范围内,我将循环要求下注。
在第3阶段,我将循环询问猜测是否为1-6或无效
如果用户始终使用有效的猜测回答,则我的以下代码有效:
def roll():
return [random.randrange(1,6), random.randrange(1,6), random.randrange(1,6)]
# Returns positive [betamount] or negative [betamount] depending on if guess is in diceroll list
def computeBetResult(diceRolls, betAmount, guessed):
return (int(betAmount) if (int(guessed) in diceRolls) else -1*int(betAmount)) if (int(betAmount) > 0) else 0
# PART 2 - prompt user input and continually ask for new bets and guesses, until user says to quit
def main():
money = 100
userAnswer = 'y'
print('Welcome to Gambling.')
while(userAnswer.strip().lower() == 'y'):
bet = input('You have $' + str(money) + '. How much would you like to bet?')
while(bet.strip().isnumeric() and int(bet) > 0 and int(bet) <= money):
guess = input('What number are you betting on? (number 1-6)')
while (int(guess) >= 1 and int(guess) <= 6):
print("Ok. You bet $" + str(bet).strip() + ' on the number ' + str(guess))
# Actually calculate the roll
theRoll = roll()
print('You rolled: ' + str(theRoll[0]) + ', ' + str(theRoll[1]) + ', ' + str(theRoll[2]))
if (int(computeBetResult(theRoll, bet, guess)) > 0):
print('You won your bet!')
money += int(bet)
else:
print('You lost your bet :(')
money -= int(bet)
print('You now have $' + str(money).strip())
# Prompt again
userAnswer = input('Would you like to play again (y/n)?')
break
break
我试过了
if (int(guess) < 1 or int(guess) > 6):
guess = input('What number are you betting on? (number 1-6)')
if(int(guess)<1或int(guess)>6:
猜测=输入('您下注的是什么号码?(号码1-6)')
在外部while循环的最末端,但这会导致不必要地要求猜测
我的设置都错了吗?或者我该如何解决这个问题
更新的尝试:
def main():
money = 100
userAnswer = 'y'
print('Welcome to Gambling.')
while(userAnswer.strip().lower() == 'y'):
bet = input('You have $' + str(money) + '. How much would you like to bet?')
while(bet.strip().isnumeric() and int(bet) > 0 and int(bet) <= money):
guess = input('What number are you betting on? (number 1-6)')
while (int(guess) >= 1 and int(guess) <= 6):
print("Ok. You bet $" + str(bet).strip() + ' on the number ' + str(guess))
# Actually calculate the roll
theRoll = roll()
print('You rolled: ' + str(theRoll[0]) + ', ' + str(theRoll[1]) + ', ' + str(theRoll[2]))
if (int(computeBetResult(theRoll, bet, guess)) > 0):
print('You won your bet!')
money += int(bet)
else:
print('You lost your bet :(')
money -= int(bet)
print('You now have $' + str(money).strip())
# Prompt again
userAnswer = input('Would you like to play again (y/n)?')
break
if(userAnswer.strip().lower() != 'y'):
break
def main():
钱=100
userAnswer='y'
打印(‘欢迎参加赌博’)
而(userAnswer.strip().lower()=='y'):
bet=input('您有$'+str(钱)+'。您想下注多少?')
而(bet.strip().isnumeric()和int(bet)>0和int(bet)=1和int(guess)0):
打印(“您赢了您的赌注!”)
货币+=整数(下注)
其他:
打印('您输了赌注:(')
货币-=整数(下注)
print('您现在有$'+str(money.strip())
#再次提示
userAnswer=input('您想再次播放吗(y/n)?')
打破
如果(userAnswer.strip().lower()!='y'):
打破
更改循环中请求有值数字的while条件,并将其放入实际循环中,以便当该数字无效时,它请求另一个数字
由于@AMC的注释,在条件下进行了编辑
编辑以放置解决问题的整个代码:
def main():
money = 100
userAnswer = 'y'
print('Welcome to Gambling.')
while(userAnswer.strip().lower() == 'y'):
bet = input('You have $' + str(money) + '. How much would you like to bet?')
while(bet.strip().isnumeric() and int(bet) > 0 and int(bet) <= money):
guess = input('What number are you betting on? (number 1-6)')
while true:
if int(guess) < 1 or int(guess > 6):
guess = input("Please choose a valid number") #will keep asking for a valid number if it is wrong
continue
else:
print("Ok. You bet $" + str(bet).strip() + ' on the number ' + str(guess))
# Actually calculate the roll
theRoll = roll()
print('You rolled: ' + str(theRoll[0]) + ', ' + str(theRoll[1]) + ', ' + str(theRoll[2]))
if (int(computeBetResult(theRoll, bet, guess)) > 0):
print('You won your bet!')
money += int(bet)
else:
print('You lost your bet :(')
money -= int(bet)
print('You now have $' + str(money).strip())
# Prompt again
userAnswer = input('Would you like to play again (y/n)?')
if userAnswer=="n":
break
def main():
钱=100
userAnswer='y'
打印(‘欢迎参加赌博’)
而(userAnswer.strip().lower()=='y'):
bet=input('您有$'+str(钱)+'。您想下注多少?')
而(bet.strip().isnumeric()和int(bet)>0和int(bet)6):
guess=input(“请选择一个有效的数字”)#如果输入错误,将继续要求输入一个有效的数字
持续
其他:
打印(“好的,你下注$”+str(bet).strip()+“在数字上”+str(猜))
#实际计算滚动
theRoll=roll()
打印('youroll:'+str(theRoll[0])+'、'+str(theRoll[1])+'、'+str(theRoll[2]))
如果(int(计算结果(旋转、下注、猜测))>0:
打印(“您赢了您的赌注!”)
货币+=整数(下注)
其他:
打印('您输了赌注:(')
货币-=整数(下注)
print('您现在有$'+str(money.strip())
#再次提示
userAnswer=input('您想再次播放吗(y/n)?')
如果userAnswer==“n”:
打破
快速浏览一下,您的问题似乎在于,当您输入一个无效的猜测数字时,您永远不会进入第三个也是最后一个循环
guess = input('What number are you betting on? (number 1-6)')
while (int(guess) >= 1 and int(guess) <= 6):
guess=input('你赌的是什么号码?(号码1-6)'
while(int(guess)>=1和int(guess)6
则您将永远不会进入while循环并直接跳转到第二个while循环末尾的中断
,该循环将您发送回第一个循环,并询问您希望再次下注多少
尝试删除第二个while循环中的
中断
,看看会发生什么,逻辑当前不是您想要的。好的-这是正确的方法:
money = 100
userAnswer = 'y'
print('Welcome to Gambling.')
while userAnswer.strip().lower() == 'y':
while True:
bet = int(input('You have $' + str(money) + '. How much would you like to bet?'))
if bet <=0 or bet > money:
print('Invalid bet - bet must be greater than 0 and less than '+str(money))
continue
# Valid bet entered
break
while True:
guess = int(input('What number are you betting on? (number 1-6)'))
if guess < 1 or guess > 6:
print('Invalid guess - you must enter a value 1-6')
continue
# Valid guess entered
break
print("Ok. You bet $" + str(bet).strip() + ' on the number ' + str(guess))
# Actually calculate the roll
theRoll = roll()
print('You rolled: ' + str(theRoll[0]) + ', ' + str(theRoll[1]) + ', ' + str(theRoll[2]))
if (int(computeBetResult(theRoll, bet, guess)) > 0):
print('You won your bet!')
else:
print('You lost your bet :(')
money += int(computeBetResult(theRoll, bet, guess))
userAnswer = input('Would you like to play again (y/n)?')
money=100
userAnswer='y'
打印(‘欢迎参加赌博’)
而userAnswer.strip().lower()=“y”:
尽管如此:
bet=int(输入('youhave$'+str(money)+'。您想下注多少?'))
如果下注:
打印('无效下注-下注必须大于0且小于'+str(货币))
持续
#已输入有效赌注
打破
尽管如此:
guess=int(输入('您下注的是什么号码?(数字1-6)'))
如果猜测<1或猜测>6:
打印('无效猜测-必须输入值1-6')
持续
#输入有效猜测
打破
打印(“好的,你下注$”+str(bet).strip()+“在数字上”+str(猜))
#实际计算滚动
theRoll=roll()
打印('youroll:'+str(theRoll[0])+'、'+str(theRoll[1])+'、'+str(theRoll[2]))
如果(int(计算结果(旋转、下注、猜测))>0:
打印(“您赢了您的赌注!”)
其他:
打印('您输了赌注:(')
货币+=整数(计算结果(掷骰子、下注、猜测))
userAnswer=input('您想再次播放吗(y/n)?')
我投票将此问题作为主题外的问题结束,因为它太宽泛了。这是因为您在代码段的最后一行有一个中断。如果输入不在1和6之间,它将从中间while循环中断。您需要更改逻辑。问题是您的第二个中断
-最后一行中的一个。它会一直执行在最内层循环后y时间,退出中间循环,返回最外层循环的开头。变量和函数名应在小写字母后面加上下划线样式。@Błotosmętek好的,取消了中断,但现在仍然有问题。.检查我的编辑为什么使用while(1):
而不是,而True:
,因为您是j
money = 100
userAnswer = 'y'
print('Welcome to Gambling.')
while userAnswer.strip().lower() == 'y':
while True:
bet = int(input('You have $' + str(money) + '. How much would you like to bet?'))
if bet <=0 or bet > money:
print('Invalid bet - bet must be greater than 0 and less than '+str(money))
continue
# Valid bet entered
break
while True:
guess = int(input('What number are you betting on? (number 1-6)'))
if guess < 1 or guess > 6:
print('Invalid guess - you must enter a value 1-6')
continue
# Valid guess entered
break
print("Ok. You bet $" + str(bet).strip() + ' on the number ' + str(guess))
# Actually calculate the roll
theRoll = roll()
print('You rolled: ' + str(theRoll[0]) + ', ' + str(theRoll[1]) + ', ' + str(theRoll[2]))
if (int(computeBetResult(theRoll, bet, guess)) > 0):
print('You won your bet!')
else:
print('You lost your bet :(')
money += int(computeBetResult(theRoll, bet, guess))
userAnswer = input('Would you like to play again (y/n)?')