Soal-Soal Latihan Python + Jawab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

I.

BASICS

1. Ask for the user’s first name and display the output message Hello [First Name]

2. Ask for the user’s first name and then ask for their surname and display the output
message Hello [First Name] [Surname].

3. Write code that will display the joke “What do you call a bear with no teeth?” and on
the next line display the answer “A gummy bear!” Try to create it using only one line
of code.

4. Ask the user to enter two numbers. Add them together and display the answer as The
total is [answer].

5. Ask the user to enter three numbers. Add together the first two numbers and then
multiply this total by the third. Display the answer as The answer is [answer].

6. Ask how many slices of pizza the user started with and ask how many slices they have
eaten. Work out how many slices they have left and display the answer in a user-
friendly format.
7. Ask the user for their name and their age. Add 1 to their age and display the output
[Name] next birthday you will be [new age].

8. Ask for the total price of the bill, then ask how many diners there are. Divide the total
bill by the number of diners and show how much each person must pay.

9. Write a program that will ask for a number of days and then will show how many
hours, minutes and seconds are in that number of days.

10. There are 2,204 pounds in a kilogram. Ask the user to enter a weight in kilograms and
convert it to pounds.

11. Task the user to enter a number over 100 and then enter a number under 10 and tell
them how many times the smaller number goes into the larger number in a user-
friendly format.
II. IF STATEMENTS

12. Ask for two numbers. If the first one is larger than the second, display the second
number first and then the first number, otherwise show the first number first and then
the second.

13. Ask the user to enter a number that is under 20. If they enter a number that is 20 or
more, display the message “Too high”, otherwise display “Thank you”.

14. Ask the user to enter a number between 10 and 20 (inclusive). If they enter a number
within this range, display the message “Thank you”, otherwise display the message
“Incorrect answer”.

15. Ask the user to enter their favourite colour. If they enter “red”, “RED” or “Red”
display the message “I like red too”, otherwise display the message “I don’t like
[colour], I prefer red”.

16. Ask the user if it is raining and convert their answer to lower case so it doesn’t matter
what case they type it in. If they answer “yes”, ask if it is windy. If they answer “yes”
to this second question, display the answer “It is too windy for an umbrella”,
otherwise display the message “Take an umbrella”. If they did not answer yes to the
first question, display the answer “Enjoy your day”.

17. Ask the user’s age. If they are 18 or over, display the message “You can vote”, if they
are aged 17, display the message “You can learn to drive”, if they are 16, display the
message “You can buy a lottery ticket”, if they are under 16, display the message
“You can go Trick- or-Treating”.

18. Ask the user to enter a number. If it is under 10, display the message “Too low”, if
their number is between 10 and 20, display “Correct”, otherwise display “Too high”.

19. Ask the user to enter 1, 2 or 3. If they enter a 1, display the message “Thank you”, if
they enter a 2, display “Well done”, if they enter a 3, display “Correct”. If they enter
anything else, display “Error message”.
III. STRINGS

20. Ask the user to enter their first name and then display the length of their name.

21. Ask the user to enter their first name and then ask them to enter their surname. Join
them together with a space between and display the name and the length of whole
name.

22. Ask the user to enter their first name and surname in lower case. Change the case to
title case and join them together. Display the finished result.

23. Ask the user to type in the first line of a nursery rhyme and display the length of the
string. Ask for a starting number and an ending number and then display just that
section of the text (remember Python starts counting from 0 and not 1).
24. Ask the user to type in any word and display it in upper case.

25. Ask the user to enter their first name. If the length of their first name is under five
characters, ask them to enter their surname and join them together (without a space)
and display the name in upper case. If the length of the first name is five or more
characters, display their first name in lower case.

26. Pig Latin takes the first consonant of a word, moves it to the end of the word and adds
on an “ay”. If a word begins with a vowel you just add “way” to the end. For
example, pig becomes igpay, banana becomes ananabay, and aadvark becomes
aadvarkway. Create a program that will ask the user to enter a word and change it into
Pig Latin. Make sure the new word is displayed in lower case.

IV. MATHS MODULE

27. Ask the user to enter a number with lots of decimal places. Multiply this number by
two and display the answer.
28. Update program No. 27 so that it will display the answer to two decimal places.

29. Ask the user to enter an integer that is over 500. Work out the square root of that
number and display it to two decimal places.

30. Display pi (π) to five decimal places.

31. Ask the user to enter the radius of a circle (measurement from the centre point to the
edge). Work out the area of the circle (π*radius2).

32. Ask for the radius and the depth of a cylinder and work out the total volume (circle
area*depth) rounded to three decimal places.

33. Ask the user to enter two numbers. Use whole number division to divide the first
number by the second and also work out the remainder and display the answer in a
user-friendly way (e.g. if they enter 7 and 2 display “7 divided by 2 is 3 with 1
remaining”).
34. Display the following message:

1) SQUARE

2) TRIANGLE

ENTER A NUMBER

If the user enters 1, then it should ask them for the length of one of its sides and
display the area. If they select 2, it should ask for the base and height of the triangle
and display the area. If they type in anything else, it should give them a suitable error
message.

V. FOR LOOPS

35. Ask the user to enter their name and then display their name three times.

36. Alter program 035 so that it will ask the user to enter their name and a number and
then display their name that number of times.
37. Ask the user to enter their name and display each letter in their name on a separate
line.

38. Change program No. 37 to also ask for a number. Display their name (one letter at a
time on each line) and repeat this for the number of times they entered.

39. Ask the user to enter a number between 1 and 12 and then display the times table for
that number.

40. Ask for a number below 50 and then count down from 50 to that number, making sure
you show the number they entered in the output.

41. Ask the user to enter their name and a number. If the number is less than 10, then
display their name that number of times; otherwise display the message “Too high”
three times.
42. Set a variable called total to 0. Ask the user to enter five numbers and after each input
ask them if they want that number included. If they do, then add the number to the
total. If they do not want it included, don’t add it to the total. After they have entered
all five numbers, display the total.

43. Ask which direction the user wants to count (up or down). If they select up, then ask
them for the top number and then count from 1 to that number. If they select down,
ask them to enter a number below 20 and then count down from 20 to that number. If
they entered something other than up or down, display the message “I don’t
understand”.

44. Ask how many people the user wants to invite to a party. If they enter a number below
10, ask for the names and after each name display “[name] has been invited”. If they
enter a number which is 10 or higher, display the message “Too many people”.
VI. WHILE LOOP

45. Set the total to 0 to start with. While the total is 50 or less, ask the user to input a
number. Add that number to the total and print the message “The total is... [total]”.
Stop the loop when the total is over 50.

46. Ask the user to enter a number. Keep asking until they enter a value over 5 and then
display the message “The last number you entered was a [number]” and stop the
program.

47. Ask the user to enter a number and then enter another number. Add these two
numbers together and then ask if they want to add another number. If they enter “y",
ask them to enter another number and keep adding numbers until they do not answer
“y”. Once the loop has stopped, display the total.

48. Ask for the name of somebody the user wants to invite to a party. After this, display
the message “[name] has now been invited” and add 1 to the count. Then ask if they
want to invite somebody else. Keep repeating this until they no longer want to invite
anyone else to the party and then display how many people they have coming to the
party.
49. Create a variable called compnum and set the value to 50. Ask the user to enter a
number. While their guess is not the same as the compnum value, tell them if their
guess is too low or too high and ask them to have another guess. If they enter the
same value as compnum, display the message “Well done, you took [count]
attempts”.

50. Ask the user to enter a number between 10 and 20. If they enter a value under 10,
display the message “Too low” and ask them to try again. If they enter a value above
20, display the message “Too high” and ask them to try again. Keep repeating this
until they enter a value that is between 10 and 20 and then display the message
“Thank you”.

51. Using the song “10 green bottles”, display the lines “There are [num] green bottles
hanging on the wall, [num] green bottles hanging on the wall, and if 1 green bottle
should accidentally fall”. Then ask the question “how many green bottles will be
hanging on the wall?” If the user answers correctly, display the message “There will
be [num] green bottles hanging on the wall”. If they answer incorrectly, display the
message “No, try again” until they get it right. When the number of green bottles gets
down to 0, display the message “There are no more green bottles hanging on the
wall”.
VII. RANDOM

52. Display a random integer between 1 and 100 inclusive.

53. Display a random fruit from a list of five fruits.

54. Randomly choose either heads or tails (“h” or “t”). Ask the user to make their choice.
If their choice is the same as the randomly selected value, display the message “You
win”, otherwise display “Bad luck”. At the end, tell the user if the computer selected
heads or tails.

55. Randomly choose a number between 1 and 5. Ask the user to pick a number. If they
guess correctly, display the message “Well done”, otherwise tell them if they are too
high or too low and ask them to pick a second number. If they guess correctly on their
second guess, display “Correct”, otherwise display “You lose”.
56. Randomly pick a whole number between 1 and 10. Ask the user to enter a number and
keep entering numbers until they enter the number that was randomly picked.

57. Update program 056 so that it tells the user if they are too high or too low before they
pick again.
58. Make a maths quiz that asks five questions by randomly generating two whole
numbers to make the question (e.g. [num1] + [num2]). Ask the user to enter the
answer. If they get it right add a point to their score. At the end of the quiz, tell them
how many they got correct out of five.

59. Display five colours and ask the user to pick one. If they pick the same as the program
has chosen, say “Well done”, otherwise display a witty answer which involves the
correct colour, e.g. “I bet you are GREEN with envy” or “You are probably feeling
BLUE right now”. Ask them to guess again; if they have still not got it right, keep
giving them the same clue and ask the user to enter a colour until they guess it
correctly.

VIII. TUPLES, LISTS, AND DICTIONARY


60. Create a tuple containing the names of five countries and display the whole tuple. Ask
the user to enter one of the countries that have been shown to them and then display
the index number (i.e. position in the list) of that item in the tuple.

61. Add to program No. 60 to ask the user to enter a number and display the country in
that position.

62. Create a list of two sports. Ask the user what their favourite sport is and add this to
the end of the list. Sort the list and display it.

63. Create a list of six school subjects. Ask the user which of these subjects they don’t
like. Delete the subject they have chosen from the list before you display the list
again.

64. Ask the user to enter four of their favourite foods and store them in a dictionary so
that they are indexed with numbers starting from 1. Display the dictionary in full,
showing the index number and the item. Ask them which they want to get rid of and
remove it from the list. Sort the remaining data and display the dictionary.
65. Enter a list of ten colours. Ask the user for a starting number between 0 and 4 and an
end number between 5 and 9. Display the list for those colours between the start and
end numbers the user input.

66. Create a list of four three-digit numbers. Display the list to the user, showing each
item from the list on a separate line. Ask the user to enter a three-digit number. If the
number they have typed in matches one in the list, display the position of that number
in the list, otherwise display the message “That is not in the list”.

67. Ask the user to enter the names of three people they want to invite to a party and store
them in a list. After they have entered all three names, ask them if they want to add
another. If they do, allow them to add more names until they answer “no”. When they
answer “no”, display how many people they have invited to the party.
68. Change program No. 67 so that once the user has completed their list of names,
display the full list and ask them to type in one of the names on the list. Display the
position of that name in the list. Ask the user if they still want that person to come to
the party. If they answer “no”, delete that entry from the list and display the list again.

69. Create a list containing the titles of four TV programmes and display them on separate
lines. Ask the user to enter another show and a position they want it inserted into the
list. Display the list again, showing all five TV programmes in their new positions.

70. Create an empty list called “nums”. Ask the user to enter numbers. After each number
is entered, add it to the end of the nums list and display the list. Once they have
entered three numbers, ask them if they still want the last number they entered saved.
If they say “no”, remove the last item from the list. Display the list of numbers.
IX. STRING MANIPULATION

71. Ask the user to enter their first name and then display the length of their first name.
Then ask for their surname and display the length of their surname. Join their first
name and surname together with a space between and display the result. Finally,
display the length of their full name (including the space).

72. Ask the user to type in their favourite school subject. Display it with “-” after each
letter, e.g. S-p-a-n-i-s-h-.

73. Show the user a line of text from your favourite poem and ask for a starting and
ending point. Display the characters between those two points.

74. Ask the user to type in a word in upper case. If they type it in lower case, ask them to
try again. Keep repeating this until they type in a message all in uppercase.
75. Ask the user to type in their postcode. Display the first two letters in uppercase.

76. Ask the user to type in their name and then tell them how many vowels are in their
name.

77. Ask the user to enter a new password. Ask them to enter it again. If the two passwords
match, display “Thank you”. If the letters are correct but in the wrong case, display
the message “They must be in the same case”, otherwise display the message
“Incorrect”.

78. Ask the user to type in a word and then display it backwards on separate lines. For
instance, if they type in “Hello” it should display as shown below:
X. NUMERIC ARRAYS

79. Ask the user for a list of five integers. Store them in an array. Sort the list and display
it in reverse order.

80. Create an array which will store a list of integers. Generate five random numbers and
store them in the array. Display the array (showing each item on a separate line).

81. Ask the user to enter numbers. If they enter a number between 10 and 20, save it in
the array, otherwise display the message “Outside the range”. Once five numbers have
been successfully added, display the message “Thank you” and display the array with
each item shown on a separate line.
82. Create an array which contains five numbers (two of which should be repeated).
Display the whole array to the user. Ask the user to enter one of the numbers from the
array and then display a message saying how many times that number appears in the
list.

83. Create two arrays (one containing three numbers that the user enters and one
containing a set of five random numbers). Join these two arrays together into one
large array. Sort this large array and display it so that each number appears on a
separate line.
84. Ask the user to enter five numbers. Sort them into order and present them to the user.
Ask them to select one of the numbers. Remove it from the original array and save it
in a new array.
85. Display an array of five numbers. Ask the user to select one of the numbers. Once
they have selected a number, display the position of that item in the array. If they
enter something that is not in the array, ask them to try again until they select a
relevant item.

86. Create an array of five numbers between 10 and 100 which each have two decimal
places. Ask the user to enter a whole number between 2 and 5. If they enter something
outside of that range, display a suitable error message and ask them to try again until
they enter a valid amount. Divide each of the numbers in the array by the number the
user entered and display the answers shown to two decimal places.
XI. 2-D LISTS AND DICTIONARIES

87. Create the following using a simple 2D list using the standard Python indexing:

88. Using the 2D list from program No. 87, ask the user to select a row and a column and
display that value.

89. Using the 2D list from program No. 87, ask the user which row they would like
displayed and display just that row. Ask them to enter a new value and add it to the
end of the row and display the row again.

90. Change your previous program to ask the user which row they want displayed.
Display that row. Ask which column in that row they want displayed and display the
value that is held there. Ask the user if they want to change the value. If they do, ask
for a new value and change the data. Finally, display the whole row again.
91. Create the following using a 2D dictionary showing the sales each person has made in
the different geographical regions:

92. Using program 92, ask the user for a name and a region. Display the relevant data.
Ask the user for the name and region of data they want to change and allow them to
make the alteration to the sales figure. Display the sales for all regions for the name
they choose.
93. Ask the user to enter the name, age and shoe size for four people. Ask for the name of
one of the people in the list and display their age and shoe size.

94. Adapt program 93 to display the names and ages of all the people in the list but do not
show their shoe size.

95. After gathering the four names, ages and shoe sizes, ask the user to enter the name of
the person they want to remove from the list. Delete this row from the data and
display the other rows on separate lines.
XII. READING AND WRITING TO A TEXT FILE

96. Write a new file called “Numbers.txt”. Add five numbers to the document which are
stored on the same line and only separated by a comma. Once you have run the
program, look in the location where your program is stored and you should see that
the file has been created. The easiest way to view the contents of the new text file on a
Windows system is to read it using Notepad.

97. Create a new file called “Names.txt”. Add five names to the document, which are
stored on separate lines. Once you have run the program, look in the location where
your program is stored and check that the file has been created properly.

98. Open the Names.txt file and display the data in Python.

99. Open the Names.txt file. Ask the user to input a new name. Add this to the end of the
file and display the entire file.
100. Display the following menu to the user:

Ask the user to enter 1, 2 or 3. If they select anything other than 1, 2 or 3 it should
display a suitable error message.

If they select 1, ask the user to enter a school subject and save it to a new file called
“Subject.txt”. It should overwrite any existing file with a new file.

If they select 2, display the contents of the “Subject.txt” file.

If they select 3, ask the user to enter a new subject and save it to the file and then
display the entire contents of the file.

Run the program several times to test the options.

101. Using the Names.txt file you created earlier, display the list of names in
Python. Ask the user to type in one of the names and then save all the names except
the one they entered into a new file called Names2.txt.
XIII. READING AND WRITING TO A CSV FILE

102. Create a .csv file that will store the following data. Call it “Books.csv”.

103. Using the Books.csv file from program 111, ask the user to enter another
record and add it to the end of the file. Display each row of the .csv file on a separate
line.
104. Using the Books.csv file, ask the user how many records they want to add to
the list and then allow them to add that many. After all the data has been added, ask
for an author and display all the books in the list by that author. If there are no books
by that author in the list, display a suitable message.

105. Using the Books.csv file, ask the user to enter a starting year and an end year.
Display all books released between those two years.
106. Using the Books.csv file, display the data in the file along with the row
number of each.

107. Import the data from the Books.csv file into a list. Display the list to the user.
Ask them to select which row from the list they want to delete and remove it from the
list. Ask the user which data they want to change and allow them to change it. Write
the data back to the original .csv file, overwriting the existing data with the amended
data.
108. Create a simple maths quiz that will ask the user for their name and then
generate two random questions. Store their name, the questions they were asked, their
answers and their final score in a .csv file. Whenever the program is run it should add
to the .csv file and not overwrite anything.
XIV. SUBPROGRAMS/FUNCTIONS

109. Define a subprogram that will ask the user to enter a number and save it as the
variable “num”. Define another subprogram that will use “num” and count from 1 to
that number.

110. Define a subprogram that will ask the user to pick a low and a high number,
and then generate a random number between those two values and store it in a
variable called “comp_num”.

Define another subprogram that will give the instruction “I am thinking of a


number...” and then ask the user to guess the number they are thinking of.

Define a third subprogram that will check to see if the comp_num is the same as the
user’s guess. If it is, it should display the message “Correct, you win”, otherwise it
should keep looping, telling the user if they are too low or too high and asking them to
guess again until they guess correctly.
111. Display the following menu to the user:

If they enter a 1, it should run a subprogram that will generate two random numbers
between 5 and 20, and ask the user to add them together. Work out the correct answer
and return both the user’s answer and the correct answer.

If they entered 2 as their selection on the menu, it should run a subprogram that will
generate one number between 25 and 50 and another number between 1 and 25 and
ask them to work out num1 minus num2. This way they will not have to worry about
negative answers. Return both the user’s answer and the correct answer.

Create another subprogram that will check if the user’s answer matches the actual
answer. If it does, display “Correct”, otherwise display a message that will say
“Incorrect, the answer is” and display the real answer.
If they do not select a relevant option on the first menu you should display a suitable
message.

112. Create a program that will allow the user to easily manage a list of names. You
should display a menu that will allow them to add a name to the list, change a name in
the list, delete a name from the list or view all the names in the list. There should also
be a menu option to allow the user to end the program. If they select an option that is
not relevant, then it should display a suitable message. After they have made a
selection to either add a name, change a name, delete a name or view all the names,
they should see the menu again without having to restart the program. The program
should be made as easy to use as possible.
113. Create the following menu:
If the user selects 1, allow them to add to a file called Salaries.csv which will store
their name and salary. If they select 2 it should display all records in the Salaries.csv
file. If they select 3 it should stop the program. If they select an incorrect option they
should see an error message. They should keep returning to the menu until they select
option 3.

114. In Python, it is not technically possible to directly delete a record from a .csv
file. Instead you need to save the file to a temporary list in Python, make the changes
to the list and then overwrite the original file with the temporary list.

Change the previous program to allow you to do this. Your menu should now look
like this:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy