1+ {
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {
6+ "id" : " view-in-github" ,
7+ "colab_type" : " text"
8+ },
9+ "source" : [
10+ " <a href=\" https://colab.research.google.com/github/Maymee614/PythonProgramming/blob/main/maymee/Ex4_Looping.ipynb\" target=\" _parent\" ><img src=\" https://colab.research.google.com/assets/colab-badge.svg\" alt=\" Open In Colab\" /></a>"
11+ ]
12+ },
13+ {
14+ "cell_type" : " markdown" ,
15+ "metadata" : {
16+ "id" : " YeTHwB3EASGn"
17+ },
18+ "source" : [
19+ " The exercises are extracted from A_Practical_Introduction_to_Python_Programming_Heinold.pdf"
20+ ]
21+ },
22+ {
23+ "cell_type" : " markdown" ,
24+ "metadata" : {
25+ "id" : " wVOW5QDgAZ_R"
26+ },
27+ "source" : [
28+ " Ex 1: Write a program that uses a while loop (not a for loop) to read through a string and print\n " ,
29+ " the characters of the string one-by-one on separate lines."
30+ ]
31+ },
32+ {
33+ "cell_type" : " code" ,
34+ "execution_count" : null ,
35+ "metadata" : {
36+ "id" : " 77KGqP-sJJNh"
37+ },
38+ "outputs" : [],
39+ "source" : [
40+ " text = \" may\"\n " ,
41+ " i = 0\n " ,
42+ " while i < len(text):\n " ,
43+ " print(text[i])\n " ,
44+ " i += 1"
45+ ]
46+ },
47+ {
48+ "cell_type" : " markdown" ,
49+ "metadata" : {
50+ "id" : " 5B-LyYV0AkCK"
51+ },
52+ "source" : [
53+ " Ex 2: A good program will make sure that the data its users enter is valid. Write a program that\n " ,
54+ " asks the user for a weight and converts it from kilograms to pounds. Whenever the user\n " ,
55+ " enters a weight below 0, the program should tell them that their entry is invalid and then ask\n " ,
56+ " them again to enter a weight. [Hint: Use a while loop, not an if statement]."
57+ ]
58+ },
59+ {
60+ "cell_type" : " code" ,
61+ "execution_count" : 1 ,
62+ "metadata" : {
63+ "id" : " gpRZnsXjArkF" ,
64+ "outputId" : " 3ec98764-090d-4330-da69-dd0f3e059558" ,
65+ "colab" : {
66+ "base_uri" : " https://localhost:8080/"
67+ }
68+ },
69+ "outputs" : [
70+ {
71+ "output_type" : " stream" ,
72+ "name" : " stdout" ,
73+ "text" : [
74+ " enter weight in kilograms: 45\n " ,
75+ " weight in puntds: 99.208\n "
76+ ]
77+ }
78+ ],
79+ "source" : [
80+ " weight_kg=-1\n " ,
81+ " while weight_kg < 0:\n " ,
82+ " weight_kg = float(input(\" enter weight in kilograms: \" ))\n " ,
83+ " if weight_kg<0:\n " ,
84+ " print(\" Invalid Input: please Try again\" )\n " ,
85+ " else:\n " ,
86+ " weight_lb = weight_kg * 2.20462\n " ,
87+ " print(f\" weight in puntds: {weight_lb: .3f}\" )\n "
88+ ]
89+ },
90+ {
91+ "cell_type" : " markdown" ,
92+ "metadata" : {
93+ "id" : " zQT0pCjRAsIK"
94+ },
95+ "source" : [
96+ " Ex 3: Write a program that allows the user to enter any number of test scores. The user indicates\n " ,
97+ " they are done by entering in a negative number. Print how many of the scores are A’s (90 or\n " ,
98+ " above). Also print out the average"
99+ ]
100+ },
101+ {
102+ "cell_type" : " code" ,
103+ "execution_count" : 2 ,
104+ "metadata" : {
105+ "id" : " Gya-rvYUAviK" ,
106+ "outputId" : " ede2eb7d-bdc7-4063-dae0-884256e50e67" ,
107+ "colab" : {
108+ "base_uri" : " https://localhost:8080/"
109+ }
110+ },
111+ "outputs" : [
112+ {
113+ "output_type" : " stream" ,
114+ "name" : " stdout" ,
115+ "text" : [
116+ " Please enter a number :91\n " ,
117+ " Please enter a number :-45\n " ,
118+ " the number of score that get A plus : 1\n " ,
119+ " the average score : 23.0\n "
120+ ]
121+ }
122+ ],
123+ "source" : [
124+ " score=1\n " ,
125+ " Aplus_count=0\n " ,
126+ " total_score=0\n " ,
127+ " count_score=0\n " ,
128+ " while score>0 :\n " ,
129+ " score=int(input(\" Please enter a number :\" ))\n " ,
130+ " if score>=90:\n " ,
131+ " Aplus_count=Aplus_count+1\n " ,
132+ " total_score=total_score+score\n " ,
133+ " count_score=count_score+1\n " ,
134+ " print(\" the number of score that get A plus : \" , Aplus_count)\n " ,
135+ " print(\" the average score : \" , total_score/count_score)"
136+ ]
137+ },
138+ {
139+ "cell_type" : " markdown" ,
140+ "metadata" : {
141+ "id" : " IHIWc5jHAwxY"
142+ },
143+ "source" : [
144+ " Ex 4: Write a program that asks the user for a string and a letter. Using a for loop, the\n " ,
145+ " program should print the index of the first occurrence of that letter and a message if the\n " ,
146+ " string does not contain the letter."
147+ ]
148+ },
149+ {
150+ "cell_type" : " code" ,
151+ "execution_count" : 3 ,
152+ "metadata" : {
153+ "id" : " xSe0mtQ8A68M" ,
154+ "outputId" : " 3d8e7257-53f7-41ab-be0e-9ab87f1fdcbd" ,
155+ "colab" : {
156+ "base_uri" : " https://localhost:8080/"
157+ }
158+ },
159+ "outputs" : [
160+ {
161+ "output_type" : " stream" ,
162+ "name" : " stdout" ,
163+ "text" : [
164+ " Please enter an string of text: hello\n " ,
165+ " A letter to search the first occurance for: e\n " ,
166+ " letter found at index1\n "
167+ ]
168+ }
169+ ],
170+ "source" : [
171+ " text = input(\" Please enter an string of text: \" )\n " ,
172+ " letter = input(\" A letter to search the first occurance for: \" )\n " ,
173+ " found_index=-1\n " ,
174+ " for index,char in enumerate(text):\n " ,
175+ " if char == letter:\n " ,
176+ " found_index=index\n " ,
177+ " break\n " ,
178+ " if found_index == -1:\n " ,
179+ " print(\" letter not found\" )\n " ,
180+ " else:\n " ,
181+ " print(f\" letter found at index{index}\" )"
182+ ]
183+ },
184+ {
185+ "cell_type" : " markdown" ,
186+ "metadata" : {
187+ "id" : " PfyUq4CnA8vk"
188+ },
189+ "source" : [
190+ " Ex 5: Write a program in which you have a list that contains seven integers that can be 0 or 1. Find the first zero entry in the list and change it to a 1. If there are no zero entries, print a message saying so."
191+ ]
192+ },
193+ {
194+ "cell_type" : " code" ,
195+ "execution_count" : 4 ,
196+ "metadata" : {
197+ "id" : " 9JuJNAwB2RXT" ,
198+ "outputId" : " c85b5e45-808a-4418-c259-17291298c0a3" ,
199+ "colab" : {
200+ "base_uri" : " https://localhost:8080/"
201+ }
202+ },
203+ "outputs" : [
204+ {
205+ "output_type" : " stream" ,
206+ "name" : " stdout" ,
207+ "text" : [
208+ " Updated list: [1, 1, 1, 1, 1, 1, 0]\n "
209+ ]
210+ }
211+ ],
212+ "source" : [
213+ " binary_list = [1,0,1,1,1,1,0]\n " ,
214+ " i = 0\n " ,
215+ " found = False\n " ,
216+ " while i < len(binary_list):\n " ,
217+ " if binary_list[i] == 0:\n " ,
218+ " binary_list[i] = 1\n " ,
219+ " \n " ,
220+ " found = True\n " ,
221+ " break\n " ,
222+ " i += 1\n " ,
223+ " print(\" Updated list: \" , binary_list)"
224+ ]
225+ }
226+ ],
227+ "metadata" : {
228+ "colab" : {
229+ "name" : " Ex4_Looping.ipynb" ,
230+ "provenance" : [],
231+ "include_colab_link" : true
232+ },
233+ "kernelspec" : {
234+ "display_name" : " Python 3" ,
235+ "name" : " python3"
236+ },
237+ "language_info" : {
238+ "codemirror_mode" : {
239+ "name" : " ipython" ,
240+ "version" : 3
241+ },
242+ "file_extension" : " .py" ,
243+ "mimetype" : " text/x-python" ,
244+ "name" : " python" ,
245+ "nbconvert_exporter" : " python" ,
246+ "pygments_lexer" : " ipython3" ,
247+ "version" : " 3.12.1"
248+ }
249+ },
250+ "nbformat" : 4 ,
251+ "nbformat_minor" : 0
252+ }
0 commit comments