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

Python Homework

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)
48 views

Python Homework

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


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: "this is my first program"


False, comments have to start with a # and the message shows up in red.
2. These are all valid names for variables
My_Name
myName
1Name
My Name
@MyName
False, as you can’t have numbers spaces or @ symbols in a variable name
3. Running this code will generate an error: Firstname = Input () print ("Hello,",
firstname)

True as the variable name is not the same as the other variable name.
4. This code would print the letter “h”
planet = "Earth"
print(planet[5])
False because the first character in python is 0 so it would need to be
print(planet[4])
5. This code will print out “15”
number = "5" + "10"
print(number)
False, the output will be 510 as this code puts 5 in front of the ten so it is 510

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:

1
Homework 1 Strings and variables
Introduction to Python
“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
My Answer
print("Please enter your first name ")
first_name = input()
print("Please enter your favourite food ")
food = input()
first_name = first_name.upper()
food = food.upper()
print(first_name + " LIKES " + food)
numchara = len(first_name + " LIKES " + food)
print("The sentence contains " + numchara + " characters")

You might also like