The assignments will be graded out of 100 points. Create a text document entitled answers.xxx (where you replace xxx with whatever extension is appropriate, depending on the file format you use). Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your name and UTA ID in the file on the first line. Your answers that are not part of a program will be added to this file. Each task below will instruct you where to put your answers.
Here is an example of a while
loop that will never terminate.
Explain why the program never stops. Put your answer in answers.xxx. Also,
modify this program so that the while loop stops after exactly 3 iterations, and save
it as task1.py.
i = 1 while_iterations = 0 while i > 0: while_iterations = while_iterations + 1 print "starting while iteration number", while_iterations for number in range(5, 10): if number % 2 == 1: print "found an odd number: ", number else: print number, "is not an odd number" print "end of for loop" print
The following code is supposed to prompt the user for a number, check to see if the number is equal to 55, and print whether it is or not. The code contains an error and will not run. In your answers.xxx file, explain what is wrong. Assume the user actually enters an integer.
user_input = raw_input("Enter an integer: ") user_input = int(user_input) if user_input = 55: print "The user entered 55." else: print "The user did not enter 55."
The following code was written using a while
loop. Rewrite it so that
it uses a for
loop, and save the program as task3.py
i = 1 while i <= 10: a = i - 1 b = i * a print b i = i + 1
In programming, it is often important to test multiple conditions with an if
statement. Write a program that asks the user for three values, a, b, and
c and checks to see if a is equal to b or a is equal to c.
If either case evaluates to True
, the program should print "True." Otherwise,
it should print "False." Save the program as task4.py.
Write a program that asks the user for an integer between 10 and 100 and prints the integer, but if an integer less than 10 or greater than 100 is entered, the program keeps asking for an integer between 10 and 100. Save the program as task5.py. Here is a sample session:
Please enter a number between 10 and 100: 123 Error, please try again. Please enter a number between 10 and 100: 5 Error, please try again. Please enter a number between 10 and 100: -12 Error, please try again. Please enter a number between 10 and 100: 15 you entered this number: 15
A square number (sometimes called a perfect square) is created by multiplying an integer by itself. For example:
So, 0, 1, 4, and 9 are perfect squares. Write a program that prompts the user for an integer from 1 and 1000, checks to make sure the user entered an integer that in that range, and prints out the perfect squares less than that number. The program should keep asking the user for an integer in that range until an appropriate integer is entered. Save your program as task6.py
Hint: You can use what you learned from Task 5.
Write a program thats prompt the user for a positive integer N and uses a
for
loop to print out the integers from 1 to N but skips every other
even number. If the user enters an invalid number, keep prompting for a new number until
a valid number is entered. Save the program as task7.py. As an example, if the user
enters 10, the output will be:
1 2 3 5 6 7 9 10
As you can see, it skipped every other even number: 4 and 8.
We often need to let a user choose an option from a menu. Write a program that asks the user for a number N to use for calculations and then presents a menu to the user like the one below:
1. Square the number 2. Multiply the number by 10 3. Divide the number by 3
The program will then ask the user to choose a menu option by entering a number from 1 to 3. Your program should have the following behavior:
Save the program as task8.py.
Pay close attention to all specifications on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for non-compliance with the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.
The assignment should be submitted via Blackboard. Submit a ZIPPED directory called assignment2.zip (no other forms of compression accepted, contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers.xxx document and all the Python code files (task5.py, task6.py, etc).