3 Replies - 745 Views - Last Post: 04 October 2012 - 06:28 AM Rate Topic: -----

#1 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

how do i get this simple code to loop?

Posted 07 March 2012 - 05:50 PM

I am having lots of trouble trying to get this code to loop... is there a easy way.... i have the number of books looping but i just cant get the discount if there a book shop or not.... like just if someone types in eg apple it just crashes... i just want it to loop till it gets a yes or no... and then work from there
def restart_program():
    pass
while True:
    try:
        books = float (input("So how many books do u want?"))
        break
    except ValueError:
        pass
    print ("that was not a number")


discount = input('do you own a book shop?(yes or no)')


book_cost = 24.95
shipping = 3
extra_shiping = 0.75
extra_discount = .6
if discount == 'yes':
    print ("$",(books * book_cost) *(extra_discount)+(books * extra_shiping) - (extra_shiping) + shipping)
elif discount == 'no':
    print ("$",(books * book_cost)+(books * extra_shiping) - (extra_shiping) + shipping)



Is This A Good Question/Topic? 0
  • +

Replies To: how do i get this simple code to loop?

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how do i get this simple code to loop?

Posted 07 March 2012 - 08:13 PM

Hey, welcome to DreamInCode. I think this little function is exactly what you want.

def userSaidYes(message):
    response = ""
    while response not in ["yes","no"]:
        response = input(str(message) +" (yes or no) ").lower()
    return response == "yes"



if the user enters "yes" it will return true, if he says no, it will return false. If he says something else, it'll just ask him again. Also, I automatically take whatever the user enters and make it lowercase. If I didn't do that "Yes" would cause it to fail. This function is fairly versatile, you might be able to use it again. For this situation, it would look like this in use userSaidYes('do you own a book shop?').

Here's a similar function I gave to another member a little while ago, you might find it useful for getting the number of books the user wants. There's a lot of comments because some people are less familiar with try/except blocks.

def getInput(inputString, expectedType, errorString = False ):
    while True: #Continue indefinitely
        try: #try to do this, but an error could hit

            #use input to communicate the inputString to the user and return the result after it's been casted to the type you're looking for
            return expectedType(input(inputString)) 

        except ValueError as e:#This is error handling
            if errorString:#if you provided an errorString
                print(errorString) #print it
            else: #otherwise
                print (str(e)) #print the default python error
            print ("Please try again\n")




if you don't mind my 2 cents, here's how I would rewrite this program to be a little cleaner
Spoiler

This post has been edited by atraub: 07 March 2012 - 08:48 PM
Reason for edit:: added spoiler tags. He might not want my 2 cents lol

Was This Post Helpful? 2
  • +
  • -

#3 littlePilot  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 22
  • Joined: 07-March 12

Re: how do i get this simple code to loop?

Posted 04 October 2012 - 12:51 AM

lol sorry about posting what u siad twice i am not used to using this web site... and i know i took awhile to thank you but i havnt had a chance... and thanks for your 2 cents since i am new to python i try and learn all i can by looking at your code... so thanks for everything :) (i wish i was as good as programing as you :P )

This post has been edited by atraub: 04 October 2012 - 06:25 AM
Reason for edit:: removed the double quote

Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: how do i get this simple code to loop?

Posted 04 October 2012 - 06:28 AM

I'm glad it helped you out!

View PostlittlePilot, on 04 October 2012 - 03:51 AM, said:

(i wish i was as good at programing as you :P )

We all have to start from somewhere. There are people out there who make me look like a bewildered jackass but we both just have to keep at it and strive to be the best we can.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1