为什么这个循环在完成之前就停止了?
Why this loop stops before it finishes?
这个掷两个骰子的小程序有一些问题。
为什么程序在完成循环之前停止,而是询问循环次数 "do you want to play again?"
感谢您的帮助!
#Program which simulates the rolling of two dice
import random
def rolling_dices(repetitions):
a = repetitions
b = 1
while b <= a:
i = (random.randrange(1,7))
y = (random.randrange(1,7))
b +=1
print(i, y, "\t =>", int(i+y))
answer = input("do you want to play again? (Y/N)")
if answer.lower() == "y":
continue
else:
break
rolling_dices(5)
确保正确缩进 while 循环:
#Program which simulates the rolling of two dice
import random
def rolling_dices(repetitions):
a = repetitions
b = 1
while b <= a:
i = (random.randrange(1,7))
y = (random.randrange(1,7))
b +=1
print(i, y, "\t =>", int(i+y))
answer = input("do you want to play again? (Y/N)")
if answer.lower() == "y":
continue
else:
break
rolling_dices(5)
python 中有关缩进的更多信息:http://www.diveintopython.net/getting_to_know_python/indenting_code.html
您似乎想从掷骰子循环中删除问题,而是将掷骰子循环放入带有问题提示的循环中。
import random
def rolling_dices(repetitions):
a = repetitions
b = 1
while b <= a:
i = (random.randrange(1,7))
y = (random.randrange(1,7))
b +=1
print(i, y, "\t =>", int(i+y))
rolling_dices(5)
while input("do you want to play again? (Y/N)").lower() == "y":
rolling_dices(5)
print("done.")
这个掷两个骰子的小程序有一些问题。
为什么程序在完成循环之前停止,而是询问循环次数 "do you want to play again?"
感谢您的帮助!
#Program which simulates the rolling of two dice
import random
def rolling_dices(repetitions):
a = repetitions
b = 1
while b <= a:
i = (random.randrange(1,7))
y = (random.randrange(1,7))
b +=1
print(i, y, "\t =>", int(i+y))
answer = input("do you want to play again? (Y/N)")
if answer.lower() == "y":
continue
else:
break
rolling_dices(5)
确保正确缩进 while 循环:
#Program which simulates the rolling of two dice
import random
def rolling_dices(repetitions):
a = repetitions
b = 1
while b <= a:
i = (random.randrange(1,7))
y = (random.randrange(1,7))
b +=1
print(i, y, "\t =>", int(i+y))
answer = input("do you want to play again? (Y/N)")
if answer.lower() == "y":
continue
else:
break
rolling_dices(5)
python 中有关缩进的更多信息:http://www.diveintopython.net/getting_to_know_python/indenting_code.html
您似乎想从掷骰子循环中删除问题,而是将掷骰子循环放入带有问题提示的循环中。
import random
def rolling_dices(repetitions):
a = repetitions
b = 1
while b <= a:
i = (random.randrange(1,7))
y = (random.randrange(1,7))
b +=1
print(i, y, "\t =>", int(i+y))
rolling_dices(5)
while input("do you want to play again? (Y/N)").lower() == "y":
rolling_dices(5)
print("done.")