Skip to content

Commit 4cd08bc

Browse files
committed
All Python For everybody Quiz and Assignment with Answers
0 parents  commit 4cd08bc

File tree

135 files changed

+42110
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+42110
-0
lines changed

.DS_Store

20 KB
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""You can write any code you like in the window below. There are three files loaded and ready for you to open if you want to do file processing: "mbox-short.txt", "romeo.txt", and "words.txt"."""
2+
3+
fh = open("words.txt", "r")
4+
5+
count = 0
6+
for line in fh:
7+
print(line.strip())
8+
count = count + 1
9+
10+
print(count,"Lines")
11+
12+
gh = open("romeo.txt", "r")
13+
14+
count = 0
15+
for line in gh:
16+
print(line.strip())
17+
count = count + 1
18+
19+
print(count,"Lines")
20+
21+
kh = open("mbox-short.txt", "r")
22+
23+
count = 0
24+
for line in kh:
25+
print(line.strip())
26+
count = count + 1
27+
28+
print(count,"Lines")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#"""2.2 Write a program that uses input to prompt a user for their name and then welcomes them. Note that input will pop up a dialog box. Enter Sarah in the pop-up box when you are prompted so your output will match the desired output."""
2+
3+
name = input("Enter Your Name: ")
4+
print("Hello "+name)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#"""2.3 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Use 35 hours and a rate of 2.75 per hour to test the program (the pay should be 96.25). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking or bad user data."""
2+
3+
4+
def computepay(h,r):
5+
if h < 0 or r < 0:
6+
return None
7+
elif h > 40:
8+
return (40*r+(h-40)*1.5*r)
9+
else:
10+
return (h*r)
11+
12+
try:
13+
hrs = raw_input("Enter Hours:")
14+
hour = float(hrs)
15+
r = raw_input("please input your rate:")
16+
rate = float(r)
17+
p = computepay(hour,rate)
18+
print (p)
19+
except:
20+
print ("Please,input your numberic")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#""" 3.1 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay the hourly rate for the hours up to 40 and 1.5 times the hourly rate for all hours worked above 40 hours. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input - assume the user types numbers properly.
2+
Grade updated on server. """
3+
4+
5+
def computepay(h,r):
6+
if h < 0 or r < 0:
7+
return None
8+
elif h > 40:
9+
return (40*r+(h-40)*1.5*r)
10+
else:
11+
return (h*r)
12+
13+
try:
14+
hrs = raw_input("Enter Hours:")
15+
hour = float(hrs)
16+
r = raw_input("please input your rate:")
17+
rate = float(r)
18+
p = computepay(hour,rate)
19+
print (p)
20+
except:
21+
print ("Please,input your numberic")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#"""3.3 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
2+
Score Grade
3+
>= 0.9 A
4+
>= 0.8 B
5+
>= 0.7 C
6+
>= 0.6 D
7+
< 0.6 F
8+
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85."""
9+
10+
11+
try:
12+
s = raw_input("please input your score:")
13+
score = float(s)
14+
if score > 1.0:
15+
print ("value out of range")
16+
elif 1.0 >= score>=.9:
17+
print ("A")
18+
elif .9 > score>=.8:
19+
print ("B")
20+
elif .8 >score>=.7:
21+
print ("D")
22+
elif .7 >score>=.6:
23+
print ("D")
24+
except:
25+
print ("Error , please input is numeric")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#"""4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Award time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of time-and-a-half in a function called computepay() and use the function to do the computation. The function should return a value. Use 45 hours and a rate of 10.50 per hour to test the program (the pay should be 498.75). You should use input to read a string and float() to convert the string to a number. Do not worry about error checking the user input unless you want to - you can assume the user types numbers properly. Do not name your variable sum or use the sum() function."""
2+
3+
def computepay(h,r):
4+
if h < 0 or r < 0:
5+
return None
6+
elif h > 40:
7+
return (40*r+(h-40)*1.5*r)
8+
else:
9+
return (h*r)
10+
11+
try:
12+
hrs = input("Enter Hours:")
13+
hour = float(hrs)
14+
r = input("please input your rate:")
15+
rate = float(r)
16+
p = computepay(hour,rate)
17+
print(p)
18+
except:
19+
print("Please,input your numberic")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#"""5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below."""
2+
3+
largest = None
4+
smallest = None
5+
6+
while True:
7+
inp = input("Enter a number: ")
8+
if inp == "done" : break
9+
try:
10+
num = float(inp)
11+
except:
12+
print("Invalid input")
13+
continue
14+
if smallest is None:
15+
smallest = num
16+
if num > largest :
17+
largest = num
18+
elif num < smallest :
19+
smallest = num
20+
21+
def done(largest,smallest):
22+
print("Maximum is", int(largest))
23+
print("Minimum is", int(smallest))
24+
25+
done(largest,smallest)
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1.When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?
2+
==> What Python statement would you like me to run?
3+
4+
2.What will the following program print out:
5+
>>> x = 15
6+
>>> x = x + 5
7+
>>> print(x)
8+
==> 20
9+
10+
3.Python scripts (files) have names that end with:
11+
==>.py
12+
13+
4.Which of these words are reserved words in Python ?
14+
==>
15+
— break
16+
— if
17+
18+
5.What is the proper way to say “good-bye” to Python?
19+
==>quit()
20+
21+
6.Which of the parts of a computer actually executes the program instructions?
22+
==> Central Processing Unit
23+
24+
7.What is "code" in the context of this course?
25+
==> A sequence of instructions in a programming language
26+
27+
8.A USB memory stick is an example of which of the following components of computer architecture?
28+
==> Secondary Memory
29+
30+
9.What is the best way to think about a "Syntax Error" while programming?
31+
==> The computer did not understand the statement that you entered
32+
33+
10.Which of the following is not one of the programming patterns covered in Chapter 1?
34+
==> Random steps
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1.Which of the following is a comment in Python?
2+
==> # This is a test
3+
4+
2.What does the following code print out?
5+
print("123" + "abc")
6+
==> 123abc
7+
8+
3.Which of the following variables is the "most mnemonic"?
9+
==> x
10+
11+
4.Which of the following is not a Python reserved word?
12+
==> spam
13+
14+
5.Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do?
15+
x = x + 2
16+
==> Retrieve the current value for x, add two to it, and put the sum back into x
17+
18+
6.Which of the following elements of a mathematical expression in Python is evaluated first?
19+
==> Parentheses ( )
20+
21+
7.What is the value of the following expression
22+
42 % 10
23+
==> 2
24+
25+
8.What will be the value of x after the following statement executes:
26+
x = 1 + 2 * 3 - 8 / 4
27+
==> 5.0
28+
29+
9.What will be the value of x when the following statement is executed:
30+
x = int(98.6)
31+
==> 98
32+
33+
10.What does the Python input() function do?
34+
==> Pause the program and read data from the user
35+
36+
11.In the following code, print(98.6) What is “98.6”?
37+
==> A constant
38+
39+
12.Which of the following is a bad Python variable name?
40+
==> spam.23
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
1.What do we do to a Python statement that is immediately after an if statement to indicate that the statement is to be executed only when the if statement is true?
2+
==> Indent the line below the if statement
3+
4+
2.Which of these operators is not a comparison / logical operator?
5+
==> =
6+
7+
3.What is true about the following code segment:
8+
if x == 5 :
9+
print('Is 5')
10+
print('Is Still 5')
11+
print('Third 5')
12+
==> Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
13+
14+
4.When you have multiple lines in an if block, how do you indicate the end of the if block?
15+
==> You de-indent the next line past the if block to the same level of indent as the original if statement
16+
17+
5.You look at the following text:
18+
if x == 6 :
19+
print('Is 6')
20+
print('Is Still 6')
21+
print('Third 6')
22+
It looks perfect but Python is giving you an 'Indentation Error' on the second print statement. What is the most likely reason?
23+
==> You have mixed tabs and spaces in the file
24+
25+
6.What is the Python reserved word that we use in two-way if tests to indicate the block of code that is to be executed if the logical test is false?
26+
==>else
27+
28+
7.What will the following code print out?
29+
x = 0
30+
if x < 2 :
31+
print('Small')
32+
elif x < 10 :
33+
print('Medium')
34+
else :
35+
print('LARGE')
36+
print('All done')
37+
==> Small
38+
All done
39+
40+
8.For the following code,
41+
if x < 2 :
42+
print('Below 2')
43+
elif x >= 2 :
44+
print('Two or more')
45+
else :
46+
print('Something else')
47+
What value of 'x' will cause 'Something else' to print out?
48+
==>This code will never print 'Something else' regardless of the value for 'x'
49+
50+
9.In the following code (numbers added) - which will be the last line to execute successfully?
51+
(1) astr = 'Hello Bob'
52+
(2) istr = int(astr)
53+
(3) print('First', istr)
54+
(4) astr = '123'
55+
(5) istr = int(astr)
56+
(6) print('Second', istr)
57+
==> 1
58+
59+
10.For the following code:
60+
astr = 'Hello Bob'
61+
istr = 0
62+
try:
63+
istr = int(astr)
64+
except:
65+
istr = -1
66+
What will the value be for istr after this code executes?
67+
==>-1
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
1.Which Python keyword indicates the start of a function definition?
2+
==> def
3+
4+
2.In Python, how do you indicate the end of the block of code that makes up the function?
5+
==> You de-indent a line of code to the same indent level as the def keyword
6+
7+
3.In Python what is the input() feature best described as?
8+
==> A built-in function
9+
10+
4.What does the following code print out?
11+
def thing():
12+
print('Hello')
13+
14+
print('There')
15+
==> There
16+
17+
5.In the following Python code, which of the following is an "argument" to a function?
18+
x = 'banana'
19+
y = max(x)
20+
print(y)
21+
==> x
22+
23+
6.What will the following Python code print out?
24+
def func(x) :
25+
print(x)
26+
27+
func(10)
28+
func(20)
29+
==> 10
30+
20
31+
32+
7.Which line of the following Python program will never execute?
33+
def stuff():
34+
print('Hello')
35+
return
36+
print('World')
37+
38+
stuff()
39+
==> print ('World')
40+
41+
8.What will the following Python program print out?
42+
def greet(lang):
43+
if lang == 'es':
44+
return 'Hola'
45+
elif lang == 'fr':
46+
return 'Bonjour'
47+
else:
48+
return 'Hello'
49+
50+
print(greet('fr'),'Michael')
51+
==>Bonjour Michael
52+
53+
9.What does the following Python code print out? (Note that this is a bit of a trick question and the code has what many would consider to be a flaw/bug - so read carefully).
54+
def addtwo(a, b):
55+
added = a + b
56+
return a
57+
58+
x = addtwo(2, 7)
59+
print(x)
60+
==>2
61+
62+
10.What is the most important benefit of writing your own functions?
63+
==>Avoiding writing the same non-trivial code more than once in your program

0 commit comments

Comments
 (0)