0% found this document useful (1 vote)
1K views2 pages

Python 1 Homework 1 Answers

Uploaded by

Thomas Webb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
1K views2 pages

Python 1 Homework 1 Answers

Uploaded by

Thomas Webb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
  • Homework 1: Strings and Variables

Homework 1 Strings and variables

Introduction to Python

Homework 1: Strings and variables Answers


True or False?
State for each question, whether the statements are True or False, and the reason why.

1. This line of code is a program comment:

False, comments are preceeded by a # and


appear in red
[2]

2. These are all valid names for variables

 My_Name
 myName
False, only the first two are valid
 1Name
 My Name
 @MyName
[2]

3. Running this code will generate an error:

True as the variable name is not the same –


variable names are case-sensitive
[2]

4. This code would print the letter “h”


False, the first character position is 0, so the
code to print the last letter “h” would be
[2] print(planet[4])

5. This code will print out “15”

False, the output will be 510 as this code is


linking strings together, more on this in the
[2] next lesson!

1
Homework 1 Strings and variables
Introduction to Python
6. Write a program which will allow a user to enter their first name, and their favourite food.
The program will then print out the sentence which includes the data they input, all in
uppercase. For example:
“HENRY LIKES STRAWBERRIES”
Then print out the number of characters in the sentence (including spaces), e.g.:
“The sentence contains 24 characters” [5]

In your program code, you may need to make use of one or more of the following
functions: len(), upper(), str()
For example:
notice = "No Entry"
numletters = len(notice) #puts 5 in numletters
newNotice = [Link]() #puts "NO ENTRY" in newNotice
number = 12345
numberAsString = str(number) #puts "12345" in number as string

#L1 HW1 Question [Link]


print("Please enter your first name: ")
firstname = input()
print("Please enter your favourite food: ")
food = input()
firstname = [Link]()
food = [Link]()
print(firstname, "LIKES", food)
numChars = len(firstname + " LIKES " + food)
print("The sentence contains", numChars, "characters")

There are many other ways of correctly coding this program!


(See program L1 HW1 Question [Link] in folder)

[Total 15 marks]

Homework 1 Strings and variables
Introduction to Python
Homework 1: Strings and variables Answers
True or False? 
State for e
Homework 1 Strings and variables
Introduction to Python
6.
Write a program which will allow a user to enter their first name,

You might also like