Download random and function part 2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Task 1 (12 minutes)
Question 1
Which of the following are syntactically correct strings?
'Hello'
'She shouted "Hello!" very loudly.'
"Goodbye'
"Hello
"Hello, world."
Question 2
To display a value in the console, what Python keyword do you use?
Answer:
Question 3
In the following code, the one line starting with
line mean to Python?
tax_rate = 0.15
income = 40000
deduction = 10000
# Calculate income taxes
tax = (income - deduction) * tax_rate
print tax
This text is printed on the console.
#
is highlighted in red. What does this
This is a syntax error.
The text is stored in a special variable called # .
This text is used as a file name for the code.
This is a comment aimed at the human reader. Python ignores such comments.
Question 4
Which of the following arithmetic expressions are syntactically correct?
9 - (2 - (4 * 3)
5 - 1 - 3 - 7 - 0
3 * ((2 - 9) + 4)) * (2 + (1 - 3))
(8 + (1 + (2 * 4) - 3))
8 / -2
Question 5
You would like to make it so that the variable
ounces
has the value 16, thus representing
one pound. What simple Python statement will accomplish this?
ounces := 16
ounces = 16
16 = ounces
Question 6
Which of the following can be used as a variable name?
number123
ounces
my.number
MYnumber
__number__
16ounces
Question 7
Assume you have values in the variables
in
x
and
x
having the sum of the current values of
x
y.
and
Which statement(s) would result
y?
x = x + y
x = y + x
x += x + y
x += y + x
Question 8
Python file names traditionally end in what characters after a period? Don't include the
period.
Answer:
Question 9
An
if
statement can have how many
elif
parts?
else
parts?
Unlimited, i.e., 0 or more
0
1
Question 10
An
if
statement can have how many
Unlimited, i.e., 0 or more
0
1
Task 2 (You may have already done this)
I have copied some bits of code below. I would like you to try the code and explain what each
bit of code is doing. There will be certain lines of code that you are not sure how it is doing
certain bits, for this, you will need to research online.
1. In particular find out about while True
2. Explain the difference between a for loop and a while loop
3. Find out what the break is doing
Next:
1. I would like you to make at least two changes to the program.
2. You may change output, the structure or anything else you feel will enhance the
program
print("Enter q to quit")
while True:
num = input("Enter a number")
if num =="q":
break
try:
num = int(num)
for i in range(1, 14):
if i < 10:
print('', end = '')
print("{0} times {1} is {2} ." .format(i, num, i*num))
except ValueError:
print("You need to enter a whole number")
Links:
https://docs.python.org/3.3/library/string.html
https://wiki.python.org/moin/WhileLoop
http://www.tutorialspoint.com/python/python_while_loop.htm
Task 4 research
Look online an find an example of for loop. Explain how it works and try to make a basic
program that will incorporate a for loop.
Task 3 challenge
I will demonstrate the program as well but this is a challenge set that will get you to research
things. If you don’t understand, work with someone and try to find information online.
Import the random module
Declare a list to be called fruit that will hold the following [“apple, banana, cherry, mango”]
Def a function called getRandomWords that passes the variable wordlist
The function should return the following random.choice(wordlist)
declare a variable called attempt to be equal to 0
declare while attempt is less than 0
the program should ask for input to be equal to guess
the variable guess should be converted into string
the variable should then convert all the text inside of it to lower
an if statement should be declared to check that the length of the word inputted is not less
than 3 letters
an appropriate message to be displayed if it is
declare a variable to pass a variable to the function
create an if statement to check if the user has guessed the word right