## 必须告诉用户将B更改为A所需的翻转次数的代码无法正常工作内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (6)

``````Enter the row and the side of the pancakes A/B): BBBB
How many pancakes can flipped at one time? 2
It took 0 flips.
Would you like to run this program again?
``````

``````Enter the row and the side of the pancakes (A/B): BBBB
How many pancakes can flipped at one time? 2
``````

``````i = True
flips = 0

while i == True:
pancakes = list(input('Enter the row and the side of the pancakes (A/B): '))
flipper = int(input('How many pancakes can be flipped at one time? '))

i = False
if 'O' in pancakes:
flips = flips + 1
for x in range(flipper):
if pancakes[x] == 'A':
pancakes[x] = 'B'
pancakes = (''.join(pancakes))

if flips == 1:
print('It took 1 flip.')
play = input("Would you like to run this program again? ")
if play == 'Yes' or play == 'yes' or play == 'Y' or play == 'y':
i = True
else:
quit()

if flips == 0:
print('It took', flips, 'flip.')
play = input("Would you like to run this program again? ")
if play == 'Yes' or play == 'yes' or play == 'Y' or play == 'y':
i = True
else:
quit()

if flips > 1:
print('It took', flips, 'flip.')
play = input("Would you like to run this program again? ")
if play == 'Yes' or play == 'yes' or play == 'Y' or play == 'y':
i = True
else:
quit()
``````

Here's my code to solve this problem...

``````while True:
pancakes = input('Enter the row and the side of the pancakes (A/B): ')
flipper = int(input('How many pancakes can be flipped at one time? '))

result, possible = 0, True
for row in pancakes.split('B'):
cnt, rem = divmod(len(row), flipper)
if rem != 0:
possible = False
break
result += cnt

if possible:
print('It took %d flips.' % result)
resp = input('Would you like to run this program again? ')
else:
resp = input("This couldn't be done. Would you like to try again? ")

if resp.lower() not in ['yes', 'y']:
break
``````