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)
how do i get this simple code to loop?
Page 1 of 13 Replies - 504 Views - Last Post: 04 October 2012 - 06:28 AM
#1
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
Replies To: how do i get this simple code to loop?
#2
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.
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.
if you don't mind my 2 cents, here's how I would rewrite this program to be a little cleaner
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
#3
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
)
This post has been edited by atraub: 04 October 2012 - 06:25 AM
Reason for edit:: removed the double quote
#4
Re: how do i get this simple code to loop?
Posted 04 October 2012 - 06:28 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|