Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
744 views

Python Homework 1 Answers

Uploaded by

Thomas Webb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
744 views

Python Homework 1 Answers

Uploaded by

Thomas Webb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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 = notice.upper() #puts "NO ENTRY" in newNotice
number = 12345
numberAsString = str(number) #puts "12345" in number as string

#L1 HW1 Question 6.py


print("Please enter your first name: ")
firstname = input()
print("Please enter your favourite food: ")
food = input()
firstname = firstname.upper()
food = food.upper()
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 6.py in folder)

[Total 15 marks]

You might also like