
Looping in Python
Question: _A priest plucked some flowers from his garden and started visiting temples on his way home. He visited 7 temples in total. At each temple, he offered half of the flowers he had at that moment. After visiting all 7 temples and returning home, he was left with exactly one flower in his...
Looping in Python
Question: _A priest plucked some flowers from his garden and started visiting temples on his way home. He visited 7 temples in total. At each temple, he offered half of the flowers he had at that moment. After visiting all 7 temples and returning home, he was left with exactly one flower in his hand.
How many flowers did he originally pluck from his garden?_
Code:
flowers = 1 temples = 7 while temples > 0: temples = temples -1 flowers = flowers*2
print (flowers)
Output:
Question: _After being banned from the royal court, Tenali Raman cleverly bribed his way in to meet Krishnadevaraya.
The king, angry at Tenali’s actions, ordered a punishment of 512 belt beatings. Before entering the court, Tenali had made a deal with each security guard he bribed to share half of whatever reward he received from the king.
Following the agreement, every guard took half of the remaining punishment, one after another. In the end, Tenali returned home without receiving a single beating.
How many security guards did Tenali bribe?_
Code:
beatings = 512 security = 0
while beatings>0: beatings = beatings//2 security = security+1
print(security)
Output:
📰Originally published at dev.to
Staff Writer