Chapter 4 Exercises¶
There are 3 syntax errors in the following code. Fix it to print correctly without errors. It will print, “Your name is Carly and your favorite color is red”.
Change
"red'
to"red"
. Add an"
beforeand your favorite
. Add a space afteris
.Fix the syntax errors so it prints “My name is Sam and I am 12 years old.”
Sam
must be a string because otherwise it’s a variable. Age must be converted to a string.You will get an error if you try to run the following code. Fix the code to print correctly without errors. It should print, “Your name is Carly and your age is 5.”
Change
age
tostr(age)
.Using the variables given, modify the print statement to print
"A car travelling at 70 mph takes 2 hours to go 140 miles."
There are 3 syntax errors in the following code. Fix it to print correctly without errors. It will print your name and age.
Change
Name
toname
. Add a+
at the end of line 3. Add a+
before and afterage
in line 4.Fix the syntax errors so that the code prints “The apple costs $5”.
Fix as shown below.
Modify line 6 to print: “The number of miles you can drive on 25 dollars is 273.97260274.”
See line 6 below. Be sure to use
str(num)
to convert a number to a string.Complete the code so that only “giant alligator” is printed.
You only want the substring from character 11 to character 26, but remember the stop sequence isn’t inclusive.
Modify line 6 to print: “You can order 40.0 wings when you have 5 people who can each spend 4 dollars and wings cost 0.5 each.”
Change line 6 to include strings that explain the value that was calculated. Be sure to use
str(num)
to convert a number to a string.Fix the code so that only “meow” is printed.
Change line 2 to the proper syntax order for getting a substring, and change the start-stop bounds to 13 to 17.
Combine lines 4 and 5 in the code below to print: “270 is 4.0 hours and 30 minutes.”
Combine lines 4 and 5 and use
+
to append strings. Usestr(num)
to convert a number to a string so that it can be appended.Complete the code on lines 3 and 4 so that it prints “2” and then “22”.
Line 3 should find “is” in sentence and line 4 should find the length of sentence 2
Complete the calculations on lines 2 and 4 and enter the items to be printed on line 5 to print the number of miles you can drive if you have a 10 gallon gas tank and are down to a quarter of a tank of gas and your car gets 32 miles per gallon. It should print: “You can go 80.0 miles.”
Calculate
numGallons
astankCapacity * 0.25
. CalculatenumMiles
asnumGallons * milesPerGallon
. Use concatenation to print out an explanation and the value. Be sure to usestr(num)
to convert a number to a string before you concatenate it.Fix line 2 so that it prints “Hi” instead of “hi”.
You have to assign
s1.capitalize()
tos1
to reflect the changeWrite code to get the name of a color from the user using the
input
function. Next convert the name of the color to all lowercase letters and print it.Use
input("message")
to get the color. Use thelower()
function to get the string as all lowercase letters.Write code to get the input of a user’s first name, then get only the first letter of their name, and print that letter lowercase.
Get the user input and get the substring of only the first character.
Write the code below to calculate and print how many months it will take to save $200 if you earn $20 a week. It should print: “It will take 2.5 months to earn 200 if you make 20 dollars a week.”
Name each of the values. Calculate the
totalWeeks
it will take and themonths
and print the information.Write code to print out the statement “Hi my name is Bob and I am 2” using only string methods or string slicing. You must get every part of the new string from the given strings.
Use string slicing to get all the words and get the length of
s1
to get the number 2. Use methods like capitalize() and lower() to convert cases.Write code below to get at least 3 values from the user using the
input
function and output a mad lib (which will use the input to tell a silly story).Use
input("message")
to get the input. Use string concatenation to print the mad lib.Write code that gets user input and print a string that states their input in all lowercase and gives the length of their string.
Use the lower and length methods on the user input. Don’t forget to convert the length to a string.