PythonExercise
前言
朋友需要帮忙解决一下
学术
问题,当然义不容辞啦 ✎~~~✐。
正文
Exercise: Day old Bread
A bakery sells loaves of bread for £ 3.49 each. Day old bread is discounted by 60 percent. Write a programme that begins by reading the number of loaves of day old bread being purchased from the user. Then your programme should display the regular price for the bread, the discount because it is a day old, and the total price. Each of these amounts should be displayed on its own line with an appropriate label. All of the values should be displayed using two decimal places, and the decimal points in all the numbers should be aligned when reasonable values are entered by the user.
1 |
|
Exercise: Length and Slicing (23)
Write a programme that asks the user to type in the first line of their favourite song and display the length of that string. The programme should also ask a starting number and an ending number and then display just that section of the text.
1 | if __name__ == "__main__": |
Exercise: Upper or Lower case name
Write a programme that asks the user to enter their first name. If the length of the first name is under five characters, the programme should ask them to enter their surname and join them together (without a space) and display the name in upper case. If the length of their first name is five or more characters, display their first name in lower case.
1 | if __name__ == "__main__": |
Exercise: Pig Latin
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 aardvark becomes aarvarkway. Create a programme 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.
1 | if __name__ == "__main__": |
Decision Making Exercises
Exercise: Umbrella or no umbrella
Write a programme that asks the user if it is raining and convert their answer to lower case so that it doesn’t matter what case they type in. If they answer “yes”, ask if it is windy. If they answer “yes” to the second question, display the answer “It’s 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”.
1 | if __name__ == "__main__": |
Exercise: Your age
Write a programme that asks the user’s age. If they are 18 or over, display the message “you can vote”. If they are 17, display the message “You can learn how to drive”. If they are 16, display the message “you can buy a lottery ticket” and if they are under 16, display the message “You can go Trick-or-Treating”.
1 | if __name__ == "__main__": |
Exercise: Even or Odd
Write a programme that reads an integer from the user. Then your programme should display a message indicating whether the integer is even or odd.
1 | if __name__ == "__main__": |
Exercise: Vowel or Consonant
In the exercise, you will create a programme that reads a letter of the alphabet from the user. If the user enters a, e, i, o or u then your programme should display a message indicating that the entered letter is a vowel. If a user enters y then your programme should display a message indicating that sometimes y is a vowel, and sometimes y is a consonant. Otherwise, your programme should display a message indicating that the letter is a consonant.
1 | if __name__ == "__main__": |
Exercise: Name the shape
Write a programme that determines the name of a shape from its number of sides. Read the number of sides from the user and then report the appropriate name as part of a meaningful message. Your programme should support shapes with anywhere from 3 to up to (and including) 10 sides. If a number of sides outside of this range is entered then your programme should display an appropriate error message.
1 | if __name__ == "__main__": |
Exercise: Classifying Triangles
A triangle can be classified based on the lengths of its sides as equilateral, isosceles or scalene. All three sides of an equilateral triangle have the same length. And isosceles triangle has two sides that are all the same length and a third side that is a different length. If all of the sides have different lengths then the triangle is scalene.
Write a programme that reads the lengths of the three sides of a triangle from the user. Then display a message that states the triangle’s type.
1 | if __name__ == "__main__": |
Exercise: Find the area
Write a programme that displays the following message:
1) Square 2) TriangleEnter a number:
If a 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 displays the area. If they type in anything else, it should give them a suitable error.
1 | if __name__ == "__main__": |
to be continued…