Skip to content

Commit 27c1b77

Browse files
authored
.
1 parent 71af1f2 commit 27c1b77

File tree

13 files changed

+272
-0
lines changed

13 files changed

+272
-0
lines changed
Loading
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("hello world")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("hello world")
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
1. When Python is running in the interactive mode and displaying the chevron prompt (>>>) - what question is Python asking you?
2+
- What would you like to do?
3+
4+
5+
2. What will the following program print out:
6+
- 20
7+
8+
9+
3. Python scripts (files) have names that end with:
10+
- .py
11+
12+
13+
4 Which of these words are reserved words in Python ?
14+
- if, break
15+
16+
17+
5. What is the proper way to say “good-bye” to Python?
18+
- quit()
19+
20+
21+
6. Which of the parts of a computer actually executes the program instructions?
22+
- CPU
23+
24+
25+
7. What is "code" in the context of this course?
26+
- A sequence of instructions in a programming language
27+
28+
29+
8. A USB memory stick is an example of which of the following components of computer architecture?
30+
- Secondary memory
31+
32+
33+
9. What is the best way to think about a "Syntax Error" while programming?
34+
- The computer did not understand the statement that you entered
35+
36+
37+
10. Which of the following is not one of the programming patterns covered in Chapter 1?
38+
- Random Steps
39+
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
hrs = input("Enter Hours:")
4+
hrs_f = float(hrs)
5+
6+
rate = input("Enter Rate:")
7+
rate_f = float(rate)
8+
9+
10+
pay = hrs_f * rate_f
11+
print("Pay:" ,pay)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1. in the following code,
2+
print(98.6)
3+
- A constant
4+
5+
6+
2.What does the following code print out?
7+
print("123" + "abc")
8+
- 123abc
9+
10+
11+
3. Which of the following is a bad Python variable name?
12+
- 23spam
13+
14+
15+
4. Which of the following is not a Python reserved word?
16+
- iterate
17+
18+
19+
5. Assume the variable x has been initialized to an integer value (e.g., x = 3). What does the following statement do?
20+
x = x + 2
21+
- Retrieve the current value for x, add two to it, and put the sum back into x
22+
23+
24+
6. Which of the following elements of a mathematical expression in Python is evaluated first?
25+
- Parentheses ( )
26+
27+
28+
7. What is the value of the following expression
29+
42 % 10
30+
- 2
31+
32+
33+
8. What will be the value of x after the following statement executes:
34+
x = 1 + 2 * 3 - 8 / 4
35+
- 5.0
36+
37+
38+
9. What will be the value of x when the following statement is executed:
39+
x = int(98.6)
40+
- 98
41+
42+
43+
10. What does the Python input() function do?
44+
- Pause the program and read data from the user
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
3+
hrs = input("Enter Hours:")
4+
h = float(hrs)
5+
6+
rate = input("Enter rate:")
7+
r = float(rate)
8+
9+
if h > 40:
10+
ext_h = h - 40
11+
ext_pay = ext_h * r * 1.5
12+
pay = 40 * r + ext_pay
13+
14+
else:
15+
pay = 40 * r
16+
print(pay)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
score = input("Enter Score: ")
12+
fscore = float(score)
13+
14+
if fscore > 1.0:
15+
print("out of range")
16+
17+
elif fscore >= 0.9:
18+
print("A")
19+
20+
elif fscore >= 0.8:
21+
print("B")
22+
23+
elif fscore >= 0.7:
24+
print("C")
25+
26+
elif fscore >= 0.6:
27+
print("D")
28+
29+
elif fscore < 0.6 :
30+
print("F")
31+
32+
33+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
5+
2. Which of these operators is not a comparison / logical operator?
6+
- =
7+
8+
9+
3. What is true about the following code segment:
10+
if x == 5 :
11+
print('Is 5')
12+
print('Is Still 5')
13+
print('Third 5')
14+
- Depending on the value of x, either all three of the print statements will execute or none of the statements will execute
15+
16+
17+
4. When you have multiple lines in an if block, how do you indicate the end of the if block?
18+
- You de-indent the next line past the if block to the same level of indent as the original if statement
19+
20+
21+
5. You look at the following text:
22+
if x == 6 :
23+
print('Is 6')
24+
print('Is Still 6')
25+
print('Third 6')
26+
- You have mixed tabs and spaces in the file
27+
28+
29+
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?
30+
- else
31+
32+
33+
7. What will the following code print out?
34+
x = 0
35+
if x < 2 :
36+
print('Small')
37+
elif x < 10 :
38+
print('Medium')
39+
else :
40+
print('LARGE')
41+
print('All done')
42+
43+
- Small
44+
All Done
45+
46+
47+
8. For the following code,
48+
if x < 2 :
49+
print('Below 2')
50+
elif x >= 2 :
51+
print('Two or more')
52+
else :
53+
print('Something else')
54+
- This code will never print 'Something else' regardless of the value for 'x'
55+
56+
57+
9. In the following code (numbers added) - which will be the last line to execute successfully?
58+
(1) astr = 'Hello Bob'
59+
(2) istr = int(astr)
60+
(3) print('First', istr)
61+
(4) astr = '123'
62+
(5) istr = int(astr)
63+
(6) print('Second', istr)
64+
- 1
65+
66+
67+
10. For the following code:
68+
astr = 'Hello Bob'
69+
istr = 0
70+
try:
71+
istr = int(astr)
72+
except:
73+
istr = -1
74+
- -1
75+
76+
77+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 4.6 Write a program to prompt the user for hours and rate per hour using input to compute gross pay. Pay should be the normal rate for hours up to 40 and time-and-a-half for the hourly rate for all hours worked above 40 hours. Put the logic to do the computation of pay 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+
4+
5+
def computepay(h,r):
6+
if h > 40:
7+
ext_h = h - 40
8+
ext_pay = ext_h * r * 1.5
9+
pay = 40 * r + ext_pay
10+
else:
11+
pay = h * r
12+
return pay
13+
14+
15+
hrs = input("Enter Hours: ")
16+
h = float(hrs)
17+
18+
rate = input("Enter Rate: ")
19+
r = float(rate)
20+
21+
pay = computepay(h,r)
22+
print("Pay",pay)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
while True:
6+
num = input("Enter a number: ")
7+
try:
8+
num = int(num)
9+
if largest is None:
10+
largest = num
11+
if smallest is None:
12+
smallest = num
13+
if num > largest:
14+
largest = num
15+
if num < smallest:
16+
smallest = num
17+
except:
18+
if num == "done":
19+
break
20+
else:
21+
print ("Invalid input")
22+
23+
print ("Maximum is", largest)
24+
print ("Minimum is", smallest)

0 commit comments

Comments
 (0)