4 Replies - 2203 Views - Last Post: 04 September 2010 - 11:13 PM Rate Topic: -----

#1 Wannabehacker  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 697
  • 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()
    


Was This Post Helpful? 0
  • +
  • -

#3 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • 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"]
        if q in allowed_answers:
            break #leaves the inner loop
        else:
            print "Illegal answer. Answer y or n please"
    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

Was This Post Helpful? 0
  • +
  • -

#4 Wannabehacker  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#5 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 697
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1