CSC 15 Lab 3 Due one week from date assigned ------------------------------------------------------------------------ Part I: This part of the assignment allows you to practice using the if-else construct. It also teaches you to think about programs in a structural way. You are to write a program that asks the user a series of questions in order to guess what kind of animal the user is talking about. The user can answer the questions with either True or False (1 or 0 can also be used) Here's a sample output of what the program might look like: Computer: Is it a mammal? True Computer: Does it walk on four legs? True Computer: Does it prey on other animals? True Computer: According to my database, your animal is likely a tiger. ---- Of course, more questions would be needed to really pin the answer down to a tiger. Your program needs to ask enough questions to distinguish between the following animals: tiger, horse, rabbit, shark, bass, turtle, snake, chicken, hawk, whale Note the above line of questions was enough to exclude all other possibilites except tiger. You should draw a species tree to determine what and how many questions you need to ask. * If the answers do not match any of the animals in your database, you are to output a message indicating so: Computer: I'm sorry, I've no information on such an animal *** Important rule: each question can only ask a single trait, i.e, you can not ask "is it a tiger" directly - that would be cheating! Try to design a program that asks a *minimal* number of questions. --- Your program will consist of a number of if-else statements nested within if-else statments. Note that commenting is very important, in addition to the indentation, to clarify the structure of the program. When writing if and if-else statements involving more than one or two lines of code, you MUST comment the end of each logical block of code. *** I WILL GRADE OR NOT HELP YOU DEBUG UNCOMMENTED CODE *** Here's something to get you started with: # Variables to use: # mammal, fly, ... print "Think of an animal among tiger, horse, rabbit, shark, bass, turtle, snake, chicken, hawk or whale" mammal = input("Is it a mammal? ") if (mammal == True): # ask next question based on answer: fly = input("can it fly> ") if (fly): # next question based on a mammal that can fly else: # next question based on a mammal that cannot fly # end mammal else: # not a mammal, next question ... ------ After you're finished with this part, put a while loop around your entire code so that the program will execute repeatedly: again = True # variable to indicate if user wants to play again while (again) # code for determining animal, excluding variable declarations again = input("go again? ") # end while ------------------------------------- What to turn in: 1. Write up on paper, including "decision tree" (I want to see this) 2. Source code. All variables used must be commented. 3. two sets of output ------------------------------------- ---------------------------------- *********************** PART II: The Main Event ************************ Implement the number guessing game where the computer does the guessing, as described in class. I've posted my version of the program where the human does the guessing. Decide on the variables you'll use, and sketch the algorithm on paper before proceeding. COMMENT CLEARLY. Output should be similar to: --------- think of a number between 0 and 1024 My guess is 512 Is the number (e)qual, (l)ess or (g)reater? g My guess is 768 Is the number (e)qual, (l)ess or (g)reater? l My guess is 640 Is the number (e)qual, (l)ess or (g)reater? l My guess is 576 Is the number (e)qual, (l)ess or (g)reater? g My guess is 608 Is the number (e)qual, (l)ess or (g)reater? l My guess is 592 Is the number (e)qual, (l)ess or (g)reater? l My guess is 584 Is the number (e)qual, (l)ess or (g)reater? g My guess is 588 Is the number (e)qual, (l)ess or (g)reater? g My guess is 590 Is the number (e)qual, (l)ess or (g)reater? l My guess is 589 Is the number (e)qual, (l)ess or (g)reater? g You cheated! Humans can't be trusted! --------- Note that your while loop stops when one of two conditions are met: 1. when the human enters e indicating that the right number has been guessed 2. when the computer decides that the human is cheating. How do you write such a loop? You can't just write while answer != "e": ... since that'll only account for the first condition. You can do the following: use a seperate boolean variable: cheated = False # will be set to true if cheating detected. while answer != "e" and cheated == false: ... Now to stop the loop, you just set cheated to True. There are other ways to stop the loop. HOWEVER, YOU MAY NOT USE ABNORMAL JUMP COMMANDS such as 'break' or 'continue'. You must terminate a loop using an appropriate boolean expression Note: to take user input for this program, it is advised to use: response = raw_input("enter e for equal, l for less than g for ...") raw_input reads everything as strings, and there is no need for the user to type the "" marks. ************************************************************************** PART III: additional problem 0. Write a program to calculate and keep track of the sales tax paid on a number of items. That is, you want to purchase a number of items and for each item, you want calculate the sales tax, as well as keep track of the total price of all items with and without tax. The output of the program should look something like the following: ------------- Enter item cost in dollars, 0 to quit: 3.99 tax amount on this item == 0.3541125 total payment amount == 4.3441125000000005 Enter item cost in dollars, 0 to quit: 12.95 tax amount on this item == 1.1493125 total payment amount == 18.443425 Enter item cost in dollars, 0 to quit: 24.99 tax amount on this item == 2.2178625 total payment amount == 45.6512875 Enter item cost in dollars, 0 to quit: 34.5 tax amount on this item == 3.0618749999999997 total payment amount == 83.2131625 Enter item cost in dollars, 0 to quit: 0 tax amount on this item == 0.0 total payment amount == 83.2131625 Total cost without tax == 76.43 Total sales tax paid == 6.78 -------------- Note that the individual tax amount and the total price with tax is printed for each iteration of the loop and the total price without tax and the total sales tax amount is printed after the loop terminates (when user enters zero for price). You need to keep track of all of these values. ALSO, the final totals should be rounded off to the nearest cent. Only the final totals should be rounded off lest there are rounding errors (.4+.4+.4 = 1.2) To write this program, first determine how many variables you need, and what each variable will be used for. ### USE COMMENTS TO CLARIFY ### for example, you should use a variable for the sales tax rate because the rate could change: rate = 0.08875 # 8.875 percent NY state sales tax rate (1.0 = 100 percent) To round off a float x, to 0.01, you can use int(x*100+0.5)/100.0 WORK OUT ALL THE MATH you need to do on paper before coding. Here's a very simple loop that just computes the sale tax: amount = 1 # set to something that's not zero, otherwise while loop won't start while amount != 0: amount = float(input("enter price, 0 to quit: ")): print("sales tax is", amount*rate) # end while But as described above, there are several other pieces of information that you need to calculate and keep track of while you run the loop. So make sure you think carefully about what will be needed.