Programming With Python (Homework) Lecture 2 Part A: Investigate: What is the difference between a string and an integer/float? What makes a float different from an integer? Given a variable, x, what is the command to get python to print out the type of x? What is a keyword? What is a statement? Give 2 examples. What is an expression? Give 2 examples. What is the difference between a statement and an expression? The following statement produces no output when not run in the shell. 3.14 * 6 * 6 Modify it to produce output. What is an operator? What problem arises in integer division? I want to divide 1 by 3 and get a result like: 0.333333333333. What is the exact command to do this? HINT: you must use floats! What do floats have that integers do not have? Integer division always rounds which way? (up or down) What is concatenation, and on what type of variables (integers/floats/strings) does it operate on? Why do we use comments? (what is their purpose) Enter the following code: >>> 1 == 1 >>> "1" == "1" >>> 1 == "1" Why is the third line false, while the first 2 lines are true? Practice Problems Programming Do as Much as you can for next class 1. a. Write a program that converts Celsius to Fahrenheit and the other way. b. Create a program that creates a table of Celsius and Fahrenheit temperatures between 0 and 100 degrees Celsius. 2. Write a program that calculates the Fibonacci sequence for values 10, 20, 50, 100, 1000, 1000000? The value should be given as input to program. A Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13 etc. n = [n-1] + [n-2] 3. Write a program to sort exactly three numbers using only if-then-else statements. Can read from file or can give to program in code or can feed in with read statement three times. 4. Ask user for a number and draw a square with that number being the number of rows and columns that compose each side of the square. make the square solid. Then make it hollow. 5. Guessing game The computer will pick a number between 1 and 100. (You can choose any high number you want.) The purpose of the game is to guess the number the computer picked in as few guesses as possible. The user will enter his or her guess until the correct number is guessed. The program will keep asking the user to guess until he or she gets the number correct. Then the program will print how many guesses were required. Sample session Time to play a guessing game. Enter a number between 1 and 100: 62 Too high. Try again: 32 Too low. Try again: 51 Too low. Try again: 56 Congratulations! You got it in 4 guesses. 6. Given a string, determine if its a palindrome. A palindrome is a string that reads the same forwards and backwards. 7. Write a program that reads text and prints out the frequencies of all the words in the text. 8.Math Quiz Description The following program runs a math quiz consisting of 10 multiplication problems involving operands between 1 and 10: from random import randint correct = 0 for i in range(10): n1 = randint(1, 10) n2 = randint(1, 10) prod = n1 * n2 ans = input("What's %d times %d? " % (n1, n2)) if ans == prod: print "That's right -- well done.\n" correct = correct + 1 else: print "No, I'm afraid the answer is %d.\n" % prod print "\nI asked you 10 questions. You got %d of them right." % correct print "Well done!" Your mission will be to do the following: Modify the program so that the user can choose how many questions they will be asked. Add levels to the program: Beginner - with operands between 1 and 10 Intermediate - with operands between 1 and 25 Advanced - with operands between 1 and 100 Modify the message at the end so that it says: Well done!: if the user answered more than 2/3 of the questions correctly. You need more practice: if they get between 1/3 and 2/3 of the questions correct. Please ask your math teacher for help!: if they get less than 1/3 of the questions correct. Allow the user to start another quiz without restarting the program. Let the user choose the question type: addition, subtraction, multiplication, or mixed. Extra for Experts: Add division as a question type, but only generate questions which have integral solutions. Maintain session totals for each type of question (the total number of questions, and the total answered correctly; the total number of addition, subtraction, multiplication, and division questions and the total correct for each of these question types).