# Chapter 4 Exercises¶

1. 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 " before and your favorite. Add a space after is.

2. 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.

3. 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 to str(age).

4. Using the variables given, modify the print statement to print "A car travelling at 70 mph takes 2 hours to go 140 miles."

5. 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 to name. Add a + at the end of line 3. Add a + before and after age in line 4.

6. Fix the syntax errors so that the code prints “The apple costs $5”. Fix as shown below. Show Comments 7. 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. Show Comments 8. 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. Show Comments 9. 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. Show Comments 10. 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. Show Comments 11. 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. Use str(num) to convert a number to a string so that it can be appended. Show Comments 12. 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 Show Comments 13. 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 as tankCapacity * 0.25. Calculate numMiles as numGallons * milesPerGallon. Use concatenation to print out an explanation and the value. Be sure to use str(num) to convert a number to a string before you concatenate it. Show Comments 14. Fix line 2 so that it prints “Hi” instead of “hi”. You have to assign s1.capitalize() to s1 to reflect the change Show Comments 15. Write 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 the lower() function to get the string as all lowercase letters. Show Comments 16. 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. Show Comments 17. 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 the months and print the information.

18. 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.

19. 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.