# Use of Loop in Python!

Page 1 of 1

## 4 Replies - 4010 Views - Last Post: 04 September 2010 - 11:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=188618&amp;s=08a337af2f395744a83e74a3ed020c2a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

# Use of Loop in Python!

Posted 01 September 2010 - 08:42 AM

Hello,

As input for program receipt, a cashier enters in the data for each of the customer’s purchases. Each data group consists of an item name, the price of the item, and the quantity of that item being purchased. The data sentinel xyz is used instead of an item name. Here is the screen display for a typical run that would also produce a sales receipt.

Sample output:
Enter item name or xyz to stop pencil
Enter price per item 2.5
Enter the quantity 3
The price is RM 7.50
Enter item name or xyz to stop scissors
Enter price per item 3
Enter the quantity 2
The price is RM 6.0
Enter item name or xyz to stop xyz

This is what I have come with so far!
```item_Name = raw_input("Enter the name of Item: ")
price = input("Enter the Price: ")
quantity = input("Enter the Quantity: ")
count=0
print "Price for ",quantity,item_Name," is ",price*quantity,"RM"
q = raw_input("Would you like to continue with the operation?(Y/N) "
while q = "Y" or "y":
q = count + 1
elif q = "N" or "n":
print "Operation has ended, Press Enter to exit"

raw_input("")

```

This post has been edited by Wannabehacker: 01 September 2010 - 08:45 AM

Is This A Good Question/Topic? 0

## Replies To: Use of Loop in Python!

### #2 Eric115

• coderさん

Reputation: 63
• Posts: 698
• Joined: 19-January 09

## Re: Use of Loop in Python!

Posted 02 September 2010 - 03:52 AM

Hi,
I had a look at your code and did what I could to patch it up. It all works for me now except I couldn't get the Y or y thing working so I took it out (changed to Y). Hope I could help!
```import sys

def main():
item_Name = raw_input("Enter the name of Item: ")
price = input("Enter the Price: ")
quantity = input("Enter the Quantity: ")
count=0
print "Price for ",quantity,item_Name," is ",price*quantity,"RM"
q = raw_input("Would you like to continue with the operation?(Y/N) ")

if q == "Y":
count = count + 1
main()
elif q == "N":
print "Operation has ended, Press Enter to exit"
sys.exit()

main()

```

### #3 Nallo

• D.I.C Regular

Reputation: 165
• Posts: 258
• Joined: 19-July 09

## Re: Use of Loop in Python!

Posted 02 September 2010 - 07:59 AM

I dont know how many times the question "how to write an input loop" has been ansewred in this forum already.
Anyway, this is the way I would write it:
```count = 0
#main loop
while True:
count += 1
print "%i th time we run through the main loop"%count
while True:
q = raw_input("again?(y/n) ")
allowed_answers = ["N", "n", "Y", "y"]
break #leaves the inner loop
else:
if q == "N" or q == "n":
break #leaves the outer loop
print "Operation has ended, Press Enter to exit"
raw_input()

```

Eric115's version has a mayor flaw. It calls main() recursively. But there is a limit on how often you can call a function recursively. Answer the continue question with Y often enough and his programm aborts with an Error when the recursion limit is reached.

This post has been edited by Nallo: 02 September 2010 - 08:01 AM

### #4 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

## Re: Use of Loop in Python!

Posted 02 September 2010 - 12:05 PM

Hello,

I Thank both of you!

### #5 Eric115

• coderさん

Reputation: 63
• Posts: 698
• Joined: 19-January 09

## Re: Use of Loop in Python!

Posted 04 September 2010 - 11:13 PM

Quote

Eric115's version has a mayor flaw. It calls main() recursively. But there is a limit on how often you can call a function recursively. Answer the continue question with Y often enough and his program aborts with an Error when the recursion limit is reached.

Oops! I forgot about the recursion limit. Sorry about that thanks for correcting me Nallo. Also just thought I should mention it, you can change the recursion limit using
```sys.setrecursionlimit(number)
```

My recursion limit is set to 1000 so I usually don't go over it.
Thanks again!