class logo

CS 122 - Quiz 2

Create python programs that solve the following problems

Here's my solution to problem 1. It is just a simple raw_input statement followed by an if statement
Problem 1 Solution

There are a lot of ways to go about solving problem 2. The basic idea is to keep multiplying the number by higher integers until you get to a value greater than or equal to 50. I saw good solutions using a while loop and good solutions using a for loop.
Problem 2 Solution

Here's my solution to problem 3.
Problem 3 Solution

The three errors were in problem 3 were:

  • if userInput = "q": should be if userInput == "q": with a double equals sign to check for equality (line 4)
  • elif n * n > targetNumber should have a : at the end of the line (line 13)
  • shouldcontinue did not have a capital C. It should have been spelled shouldContinue (line 1)

Posted Thursday, July 30

Problem 1

User input and Conditionals

Write a program that asks the user for their name. If their name is "Shad", print out "Hi professor", otherwise print out "Hi there"

Here's what a run of the program would look like if the user entered: "Shad"

What is your name? Shad Hi professor

And here's what another run would look like if the user entered: "Joe"

What is your name? Joe Hi there

 

Problem 2

Loops

Write a program that asks the user to input a number between 1 and 10 and then prints out all the multiples of that number that are less than 50. Try to match the look of the example runs exactly.

Here's what a run of the program would look like if the user entered "10":

Enter an integer between 1 and 10: 10 Multiples of 10 that are less than 50 are: 10 20 30 40

Here's what a run of the program would look like if the user entered "7":

Enter an integer between 1 and 10: 7 Multiples of 7 that are less than 50 are: 7 14 21 28 35 42 49

Here's what a run of the program would look like if the user entered "2":

Enter an integer between 1 and 10: 2 Multiples of 2 that are less than 50 are: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48

 

Problem 3

Finding errors

The following program determines whether an integer is a perfect square. There are no logic bugs in the program, but there are 2 syntax errors and 1 other error. Fix the bugs so that the program runs correctly. You can copy and paste the code from the web browser or just download it using this link

shouldcontinue = True while shouldContinue == True: userInput = raw_input("Enter an integer to see if it's a perfect square (q to quit): ") if userInput = "q": print "Goodbye" shouldContinue = False else: targetNumber = int(userInput) for n in range(targetNumber): if n * n == targetNumber: print userInput + " is a perfect square!" break elif n * n > targetNumber print userInput + " is not a perfect square" break

Here's what a sample run of the program after it has been fixed should look like:

Enter an integer to see if it's a perfect square (q to quit): 3 3 is not a perfect square Enter an integer to see if it's a perfect square (q to quit): 4 4 is a perfect square! Enter an integer to see if it's a perfect square (q to quit): 5 5 is not a perfect square Enter an integer to see if it's a perfect square (q to quit): 6 6 is not a perfect square Enter an integer to see if it's a perfect square (q to quit): 7 7 is not a perfect square Enter an integer to see if it's a perfect square (q to quit): 8 8 is not a perfect square Enter an integer to see if it's a perfect square (q to quit): 9 9 is a perfect square! Enter an integer to see if it's a perfect square (q to quit): q Goodbye

Note: that you do not need to make major changes to this program. The changes required are small. Two of the errors are invalid syntax and the other is a programming typo.

Side note, this program doesn't properly handle the case where a user enters the numbers 1 or 2, that's okay, I just wanted to keep the program as simple as possible.

 

Use this form to submit your quiz solutions. Note that just like an assignment, you can submit more than once if you realize that you made a mistake on a submission.

Student number (no dashes):
This is the 9-digit number on your student body card. It probably starts 950 or 951 and it looks something like this: 950123456. This number is used to identify your submission so please enter it carefully.
Name (first and last):
Email address:
Your solution to problem 1
Attach your python program that is your solution to just the first problem
Your solution to problem 2
Attach your python program that is your solution to just the second problem
Your solution to problem 3
Attach your python program that is your solution to just the third problem