Linguistic Gaming with Python

Python Practice 2

In this exercise, we work with variables, strings, user input and learn a bit about mathematical operators. You should read Dawson's Chapter 2. The source code for the programs he goes through can be found here.


Exercise 1:

Dawson (p.29) lists several arithmetic operators, but not all. You will have to research further to solve the following exercise.

Exercise 1.1. Write a program that calculates the following: 5 to the power of 128, 1234 times 678, 1000 divided 15 (true division).
Exercise 1.2. Make sure your program prints the results.
Exercise 1.3. Write a program that asks the user for two numbers and then calculates: 1) the integer/floor division of those numbers; 2) the remainder of a division (using the modulus operator). Print out the results (and tell the user which result represents what).

Don't forget to include plenty of comments in your program!


Exercise 2: Write a program that asks the user for his/her birth date and then runs it through the following birthday algorithm. Note that you will need to get the information about the month, the day and the year. You can think about how best to get this from the user (i.e, write your requests for input clearly and get the information in an "easy-to-use" form).

  1. Take the month of the birthday and do the following: multiply it by 4, add 13, multiply this result with 100, divide it by 4 and then subtract 200.
  2. Add the day of the birthday to this number. Multiply the result by 2, then subtract 40 and multiply it all by 50.
  3. Take the last two numbers of the year of birth (i.e., for 1975 this would be 75) and add this to the result so far.
  4. Now subtract 10 500 from the result so far and print out the final result.

If everything worked correctly, you should get back the original birth date, written in American format (and now you can ask yourself why this worked...).


Exercise 3: This program is taken from Zacharski's book on Python for Linguists (it has been modified a bit). What does it do? NB: TA in English stands for "teaching assistant".

ta = 2
quizzes = 27
to_grade = quizzes // ta
remainder = quizzes % ta
ta1 = to_grade + remainder
ta2 = to_grade
print("TA #1 should grade:",  ta1, "quizzes.")
print("TA #2 should grade:", ta2, "quizzes.")

Exercise 4: Write a program that allows a user to enter two words.

Exercise 4.1. The program should print out a compounded version. For example, "black" and "board" should result in "blackboard".
Exercise 4.2. The program should also ask the user for a number. It should print out the the first word repeated the number of times specified by the user. The second word should be printed out the number that was specified divided by two, but using integer division.


Please hand in the exercises to aikaterini-lida at uni konstanz by Friday the 7th of November by 11 am.

End