[go: up one dir, main page]

0% found this document useful (0 votes)
18 views23 pages

Solved Exercises

This document contains 14 solved exercises in Python programming. The exercises cover topics such as data input and output, variables, arithmetic operators, strings, conditionals, and mathematical functions. Each exercise presents a problem, the Python code to solve it, and an example of execution. It also includes additional example codes to illustrate concepts such as string concatenation, indexing, length, and string comparison.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

Solved Exercises

This document contains 14 solved exercises in Python programming. The exercises cover topics such as data input and output, variables, arithmetic operators, strings, conditionals, and mathematical functions. Each exercise presents a problem, the Python code to solve it, and an example of execution. It also includes additional example codes to illustrate concepts such as string concatenation, indexing, length, and string comparison.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Solved exercises

Introduction to Programming (Catholic University of Salta)

Studocu is not sponsored or endorsed by any school or university.


Downloaded by Juan Carlos Gonzalez (zonaspeedy1@gmail.com)
lOMoARcPSD|28937740

1
I wrote a program that asks the user to enter their name. The name must be
store it in a variable called name. Next, it should be displayed on the screen.
Now you are in the matrix, [user]
that the user has entered.

Example of execution:
aicP
irat
Now you are in the matrix, Patricia

Example code
1variable=23.95

Throws an error because variable names can only start with letters or
underscores (_).

Example code
n1=int(input())
n2=float(input())

We know that input() reads what the user types in the program, but the type of
that which is read will always be a string. If we need it to be a number, we must
convert what input() returns. To convert to an integer we use
int(input()) and to convert to a number with decimals we use float(input()).

2
I wrote a program that asks the user to enter a decimal number and stores it.
in a variable. Next, the program must ask the user to enter a number
whole and store it in another variable. In a third variable, the result of
the sum of the two numbers entered by the user. Finally, it should be displayed on the screen
The result of the sum is [sum]
of the operation.
Solution:
first number:
b=int(input("Second number:"))
c=a+b
print("The result of the sum is", c)

Execution example:
First number: 14.2
Second number: 19
The result of the sum is 33.2.
lOMoARcPSD|28937740

3
I wrote a program that asks the user for two numbers and stores them in two variables. In
another variable, store the result of the sum of those two numbers and then show it
result on screen.
Next, the program must ask the user to enter a third number, which will
it should be stored in a new variable. Finally, display the result on the screen.
multiplication of this new number by the result of the previous sum.

Execution example:
Enter a number: 1
Enter another number: 2
3
Enter a new number: 3
Multiplication of the sum by the last number: 9

Example code
a=a+1
a+=1

The two instructions above are equivalent (either of them does the same thing)
same: add 1 to the value stored in variable a and replace the previous value
of with the new result).

4
I wrote a program that asks the user to enter the number of kilometers traveled by
a motorcycle and the amount of liters of fuel it consumed during that trip.
Show fuel consumption per kilometer.

Execution example:
Kilómetros recorridos:260
Litros de combustible gastados:12.5
The consumption per kilometer is 20.8.

5
I wrote a program that asks the user to input a temperature in scale
Fahrenheit (should allow decimals) and show the equivalent in degrees Celsius.
The conversion formula used for this calculation is: _Celsius = (5/9) * (Fahrenheit-32) _

Example of execution:
Ingresá una temperatura expresada en Farenheit:75
23.88888888888889
lOMoARcPSD|28937740

6
I wrote a program that asks the user to enter three numbers to then show them the
average of the three.

Execution example:
First number: 8.5
Second number: 10
Third number: 5.5
The average of the three is 8.0

7
I wrote a program that asks the user for a number and subtracts 15%, storing everything
in a single variable. Next, display the final result on the screen.

Execution example:
Enter a number: 260
After applying a 15% discount, it remains: 221.0

Example code
cadena
string=string+"good"
string=string+" day"
print(string)

When the + operator is used in an operation between strings, it is being


performing a concatenation (union of strings). The print instruction shown
it will print "good day".

8
I wrote a program that asks the user to enter two words, which will be stored.
in two different variables. Next, store in a variable the concatenation of the
first word, plus a space, plus the second word. Show this result on screen.

Execution example:
knocking down
Second word: walls
knocking down walls

Example code
lOMoARcPSD|28937740

I am programming
print(sentence[0])
i=6
print(sentence[i])

The operator [ ] (brackets) allows you to obtain a character from a string.


the character position is indicated between brackets, either by entering
directly the number, with a variable that contains a number or with a
operation that results in a number. Always, the first character of a
string will be located at position 0.

Sample code
I am programming
print(len(sentence))
ultimo_caracter=frase[len(frase)-1]
print(last_character)

Using len() we can get the number of characters contained in a string.


This value will always be an integer (int type) and can be stored in a
variable, print itself, be used in an arithmetic operation, etc.

9
I wrote a program that asks the user to input text and stores that text in a
variable. Next, display on screen the first letter of the entered text. Then,
ask the user to enter a positive number less than the number of characters that
you have the text you entered (for example, if you wrote the word 'HELLO', it must be a
number between 0 and 4) and store this number in a variable called index.
Display on screen the character of the text located at the position given by index.

Execution example:
In a place in La Mancha, whose name I do not
I want to remember...
The character in first place is: E
Enter a positive number less than 63
7
The character in that position is: u

Example code
a=10
b=4
print(a != b)

The print instruction will output True, since the value contained in a is different.
of the value contained in b.
lOMoARcPSD|28937740

10
I wrote a program that asks the user to enter how many music shows they have seen.
the last year and store that number in a variable. Then display on screen a
True or False indicating if the user has watched more than 3 shows.

Execution example:
Shows viewed in the last year: 3
False

Example code
print(58273%10)
print(58273//10)

The first instruction will print the number 3, as it is the remainder of the division
from 58273 by 10. The second instruction will print 5827, since it is the integer part
the result of dividing 58273 by 10. These mathematical operations are
strategies that can be used to obtain parts of a number.

11
I wrote a program that asks the user to enter a date made up of 8 numbers,
where the first two represent the day, the next two the month, and the last four the
year (DDMMYYYY). This data must be stored in a variable of type int (integer).
Finally, show the user the date in the format DD / MM / YYYY.

Execution example:
16 November 2017
November 16, 2017

12
I wrote a program to prompt the user to enter an integer and then
print a boolean value depending on whether the number is even or not. Remember that a
A number is even if the remainder, when divided by 2, is 0.

Execution example:
Número entero:7254
True
lOMoARcPSD|28937740

Example code
a=int(input())
print(a>100 and a!=1000)

First, the logical values (True or False) of the two will be calculated.
comparisons: a > 100 and a != 1000 (which will depend on the number stored in the
variable a). Next, the truth table of the AND operation will be used.
to calculate the result.

13
I wrote a program that asks the user for their age and stores it in a variable. That then
I requested the number of items purchased in a store and stored it in another variable.
Finally, display a truth value (True or False) indicating whether the user is
over 18 years old and also bought more than 1 item.

Example of execution:
Tuedad:32
Items purchased: 1
False

14
I wrote a program that, given a string of text from the user, prints True if the
the number of characters in the string is an odd number, or False if it is not.

Execution example:
It was the best of times, it was the worst of times
times.
True

Example code
stone
drink

Both comparisons return True because the string 'animal' is less than 'piedra'.
and the string 'dance' is less than 'drink'. The order is given by how they appear
the letters in the alphabet. In the case of 'animal' and 'stone', the 'a' is less than
the 'p'. In the case of 'dance' and 'drink', since the first letter is the same it
evaluate the second, and in this case 'a' is less than 'e'.
lOMoARcPSD|28937740

15
I wrote a program that asks the user to enter two words and saves them in two
variables, and then print True if the first word is less than the second or False if
it is not.

Execution example:
A word: complexity
Another word: algorithm
False

16
I wrote a program to ask the user for their name and then the name of another person.
storing each name in a variable. Then display a truth value on the screen that
indicate if: the names of both people start with the same letter or if they end with the same letter
same letter. For example, if the entered names are María and Marcos, it will show True,
since both start with the same letter. If the names are Ricardo and Gonzalo,
it will show True, since both end with the same letter. If the names are Florencia and
Lautaro will show False, as neither the first nor the last letter match.

Execution example:
d
erA
ofl
Another name: Eduardo
True

Section 2
Blocks - Selection - Repetitions
Example code
x=10
if x!=0:
Hello

The previous program will always display 'Hello' because the condition that
the if instruction can never be False, since the variable x is 10 and 10
it will always be different from 0. If, instead of assigning a specific value, you
we ask the user to enter one and we store it in x, it will depend on whether that
value is different from 0 or not to know if the word 'Hello' will be shown or not.

17
lOMoARcPSD|28937740

I wrote a program that, given an integer, displays its absolute value. Remember that,
for positive numbers, their absolute value is equal to the number (the absolute value of 52 is
52), while for the negatives, their absolute value is the number multiplied by -1 (the
The absolute value of -52 is 52.

Execution example:
Number: -12
Absolute value: 12

Example code
x=int(input("Number:"))
if x%2==0:
The number is even
else:
The number is odd

The if-else instruction allows one block of code or another to be executed, but
never both. In this example, if it is true that the number entered by the user
(and stored in the variable x) is an even number (that is, if it is true that the
the remainder of the division of the number by 2 is 0), it will print 'The number is even'. If
that condition is false (the remainder of the division of the number by 2 is not 0), it
It will print 'The number is odd'.

18
I wrote a program that asks the user to input two different numbers and displays them.
screen to the larger of the two.

Execution example:
Un número:592
Another different number: 1726
1726 is greater

19
I wrote a program that asks the user for a letter and, if it is a vowel, displays the message
It is a vowel. Check if the user entered a string longer than one character and, in that case,
to inform you that the data cannot be processed.

Execution example:
Letter: o
It is a vowel

20
lOMoARcPSD|28937740

I wrote a program to ask the user for three numbers and display the smallest one on the screen.
the three.

Execution example:
First number: 20
Second number: 30
Third number: 10
Minor: 10

Example code
n=int(input('Number:'))

if n > 10 and n < 20:


Correct number
else:
Incorrect number

if n<10 or n>20:
Incorrect number
else:
Correct number

In the two previous if-else statements, any outcome is considered correct.


number between 10 and 20, but it is necessary to observe how the operators < and > change
and how the operator and changes to or to achieve the same objective.

21
I wrote a program that asks for a username and a password. If the
name is "Gwenevere" and the password is "excalibur", display on screen "User and
correct passwords. You can enter Camelot.
Show "Access Denied".

Execution example:
gwen
excalibur
Access denied

22
I wrote a program that determines if a year is a leap year. For a year to be a leap year
it must be divisible by 4 and it must not be divisible by 100, except when it is also divisible by
400.

Example of execution:
lOMoARcPSD|28937740

2020
Leap year

Example code
for x in range(0,10):
print(x)

for x in range(10):
print(x)

The two previous repetitions will print the same: the numbers from 0 to 9.
the first indicates that it should start at 0 and end at 9 (the range
it will always end at the given number as final, minus 1). In the second, by omitting
The number to start with is automatically taken as 0 as the beginning of the.
range. If we wanted, we could indicate that the range starts at another number that
do not be the 0.

23
I wrote a program that asks the user for an integer and displays all the numbers.
correlatives between 1 and the number entered by the user.

Execution example:
Enter a number: 3
1
2
3

24
I wrote a program that displays the sum of all numbers between 0 and 100.

Execution example:
Sumatoria: 5050

25
I wrote a program that, given a number by the user, displays all its divisors.
positive. Remember that a divisor is one that divides the number exactly (with no remainder)
0).

Execution example:
Number: 14
Divisors:
1
lOMoARcPSD|28937740

2
7
14

Example code
for x in "hello":
print(x)

The for loop uses the in operator to iterate through a sequence. This
operator returns a logical value: True if the first operand is contained in the
second, False if not. It is possible to use the in operator in a way
independent, to know, for example, if a string is contained within
"a" in "hola" will result in True because the string "a" is within
Hello. Likewise, the logical value obtained from the operation can be negated.
"a" not in "hola" will result in True because it is not true that
the string 'a' is not within 'hello'.

26
I wrote a program that, given a phrase by the user, shows the total number of vowels.
(both uppercase and lowercase) that contains.

Execution example:
Green that I love you green
Vocales: 11

27
I wrote a program that displays the first 10 numbers of the Fibonacci sequence.
the sequence begins with the numbers 0 and 1 and, from these, each element is the sum of the
the two previous numbers in the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...

Example of execution:
0
1
1
2
3
5
8
13
21
34
lOMoARcPSD|28937740

28
I wrote a program that, given a positive integer, calculates and displays its factorial.
The factorial of a number is obtained by multiplying all the positive integers that exist.
between 1 and that number. The factorial of 0 is 1.

Execution example:
Number: 7
Factorial: 5040

29
I wrote a program that allows the user to enter 6 integers, which can be
positive or negative. At the end, show the sum of the negative numbers and the
average of the positives. Don't forget that it is not possible to divide by zero, so it is
It is necessary to prevent the program from throwing an error if positive numbers were not entered.

Execution example:
Number: 15
Number: -12
Number: 5
Number: 10
Number: -3
Number:9
Sum of the negatives: -15
Average of the positives: 9.75

Example code
Phrase:
for character in string:
print(character)

When traversing a string using a for loop that iterates through each of its
characters, assign a new value to the iterator variable (in this case,
character) does not modify the original string. That is, the instruction character="a"
I would not modify the string stored in the variable cadena at all within the for.

30
I wrote a program that allows the user to enter a phrase and then a character (string of
length 1) and then display the entered phrase, but with all occurrences of the character
indicated by the user replaced by "*".

Execution example:
lOMoAR PSD|28937740

Here I start to sing to the rhythm of the vihuela


Character: o
Here I start to sing to the rhythm of the guitar.

31
I wrote a program that, given a phrase by the user, shows it reversed, without using a
slice with negative step.

Execution example:
I knew who I was this morning, but I've changed several times.
times since then
the man who drove close to the beach, near the house
are beautiful eyes

Example code
sum=0
number=int(input("Number:"))
while number != 0:
sum=sum+number
number=int(input("Number:"))
Sum of all entered numbers:

A while loop needs a condition (in this example: number != 0), which is
a logical value to determine whether to continue repeating the block or not. If the value is
True, the corresponding while block is executed; if it is False, it is not executed and
continue the program with the instruction following the block. The example code
allows entering numbers and each one accumulates by adding it to the variable sum, to
then ask the user for a new number. When 0 is entered, the block of
while does not execute and goes directly to the next instruction, which in this
case is a print that shows the sum of all the numbers entered.

Example code
number=int(input("Number:"))
while number != 0:
Your number is:

The previous repetition contains an error, which is that if the number entered by the
user is different from zero, an infinite loop will occur that will display 'Your
number is: [user number]” infinite times, without continuing with the rest of the
program. This is because, in while loops, once executed the
block, the condition is re-evaluated and, if it is True, it runs again
block and evaluate the condition again. In the case of entering a value
different from 0 in the variable number, if changing that value is not allowed then the
the condition will always compare the same number with 0 and will always be True, so
that will never stop repeating the print instruction inside the block. To avoid it,
It is necessary to allow changing the value of the variable number:

number
while number != 0:
Your number is:
lOMoARcPSD|28937740

number=int(input('Number:'))

32
I wrote a program that allows the user to enter the amounts of a customer's purchases.
(the amount of data to be loaded is unknown, which may change with each execution),
stopping data entry when the user enters the amount 0. If an amount is entered
negative, it should not be processed and it should be requested to enter a new amount. In conclusion,
inform the total to be paid considering that, if sales exceed the total amount of
1000, a 10% discount should be applied.

Execution example:
Sale amount: $100
Sales amount: $300
Amount of a sale: $-1
Invalid amount.
Amount of a sale: $2000
Sale amount: $0
$2160.0

33
I wrote a program that allows the user to enter a number of positive numbers.
undefined (the amount to be entered is unknown and can change with each execution),
finishing when the number 0 is entered (which will not be taken into account). Once finished the
reading numbers, inform which was the largest of the entered numbers.

Execution example:
Number: 6
Number: 9
Number: 2
Number: 12
Number: 0
Entered number: 12

Example code
The while loop will execute the block as long as the condition is True. In the
previous example, the block will repeat while the input strings have
length 1 (a single character) and the repetition will end when a is entered
string with length different from 1 (either an empty string or one that has more than 1)
character)

cantidad=0
lOMoARcPSD|28937740

character=input("Character:")
while len(character) != 1:
quantity=quantity+1
character=input("Character:")

In this second example, the block will repeat while the input strings
have a different length than 1 and the repetition will end when a is entered
unique character.

34
I wrote a program that asks the user if they want to analyze student grades and,
only if you answer "S" will the data processing begin, until the user enters
something different from 'S'. For each student, allow entering their grade. If it is greater than 4 the
student is approved. Finally, display "Percentage of approved students: x %" (where
x is the percentage of passing grades out of the total processed grades). It should also be
Print "Average of those approved: y" (where y is the average grade, only of the
approved students) .

Execution example:
Analyze ratings? 'Y' for 'yes':Y
Calificación de un alumno:9
Continue? 'Y' for 'yes':Y
Student rating: 4
Continue? 'Y' for 'yes': Y
Student's grade: 8
Continue? 'Y' for 'yes':N
Percentage of students passed: 66.66666666666667%
Average of those approved: 8.5

Example code
cantidad=0
n=int(input("Number:"))
while n>0 and n%10!=0:
quantity=quantity+1
n=int(input("Number:"))

A condition must be a logical value, regardless of the type of operation.


it should be done to obtain it. In the previous example, the block will be executed
as long as the entered numbers are positive (greater than 0) and are not multiples
of 10. When the user enters a number that breaches either of the two
conditions (a negative number or a multiple of 10) the block stops
execute.
lOMoARcPSD|28937740

35
I wrote a program that asks the user to input strings of length 1 (one only
character), one at a time. The repetition will end when a string that does not have is entered.
length 1, or when the entered string corresponds to the numeric digit 0. Upon completion,
show the complete string formed with all the entered characters and what percentage
of the total characters were the letter 'a'.

Execution example:
I wrote a character: L
I wrote a character:9
I wrote a character: a
I wrote a character:4
I wrote a character: A
I wrote a character:0
I wrote a character: N
I wrote a character: a
I wrote a character: a
I wrote a character:5
The complete string is: L9a4A0Naa
Porcentaje de letras ‘a’: 33.333333333333336

36
I wrote a program that, given an integer number from the user (stored as int), shows
the sum of all its digits. Remember that you will need to obtain each of the digits by
separated to be able to add them together.

Execution example:
I wrote a number: 7124
Sum of the digits: 14

37
I wrote a program to ask the user to enter positive integer numbers (the
the amount that will be entered is not known and is decided by the user). The reading of numbers will end
when the user enters the number -1. For each number entered, display the quantity of
even digits and the quantity of odd digits it has. Finally, show how many numbers
multiples of 3 the user entered.

Execution example:
Number (-1 to end the program): 123
Even digits: 1
Odd digits: 2
Number (-1 to end the program): 44
Even digits: 2
Odd digits: 0
Number (-1 to end the program):9
lOMoARcPSD|28937740

Even digits: 0
Odd digits: 1
Number (-1 to end the program): -1
Two multiples of 3 were entered.

38
I wrote a program that asks the user for a string of characters (which may contain
letters, numbers or symbols). Analyze the string to show: how many letters of the alphabet
(lowercase and uppercase) contains, how many symbols (characters that are neither letters nor
numbers), how many numerical digits, and of the digits, how many are multiples of 4.

Execution example:
String: 1984 (novel by George Orwell)
Number of letters: 20
Number of numeric digits: 4
Number of symbols: 6
Cantidad de múltiplos de 4: 2

39
I wrote a program that allows the user to input numbers which will be read as strings.
not as int or float) until one is entered that is a multiple of 10 or less than 0 (which will not be
processed). Two strings will be formed, in which the entered numbers will be concatenated,
according to the following criteria: in a string all the numbers that the user will be concatenated
enter whose number of digits is a multiple of 3. In the other, all will be concatenated
numbers that contain the digit 0. If a number meets both conditions, it must
concatenate in both strings. In each string, after each concatenated number must
insert the character '-'. Finally, display both strings on the screen.

Execution example:
829
Number: 102834
Number: 6
4307
Number: 23
1602357
Número:5896
720
Numbers whose number of digits is a multiple of 3: 829-
102834-
Números que contienen el 0: 102834-4307-1602357-

40
I wrote a program that allows the user to enter book titles via keyboard, ending
the input by reading the string "*" (asterisk). Every time the user enters a string of
a line that contains only one slash "/" is considered to end a line. For each line
lOMoARcPSD|28937740

complete, inform how many numeric digits (from 0 to 9) appeared in total (in all the
titles of books that are composed along that line). Finally, report how many complete lines
were entered.

Execution example:
Chain: Don Quixote of La Mancha
Chain: The Three Musketeers
A Tale of Two Cities
Chain:/
2 digits appear on the line
Chain: 20,000 Leagues Under the Sea
Chain: The Lord of the Rings
Chain: Alice in Wonderland
Chain: 1984
Chain: The Hobbit
Chain:/
9 digits appear on the line
Divine Comedy
Dracula
Chain:/
0 digits appear on the line
Chain: 20 years later
Chain: The Travels of Gulliver
Chain:*
3 full lines were read.

Section 3
Functions
Example code
def average(x, y, z):
return (x+y+z)/3

n1=int(input("First number:"))
n2 = int(input("Second number:"))
n2=int(input("Third number:"))
print("The average of the three is:", average(n1,n2,n3))

The sum function defined in the example takes three parameters and returns the
average.
A function ends where its block ends or when an instruction is executed.
return, even if it is anywhere in the block. Upon returning, the program
continue from the point where the function was called.
lOMoARcPSD|28937740

A function can receive 0 or more parameters, without limit, but cannot return
more than one result at a time. It can also return nothing (for example, a
function that only displays something on the screen.

41
I wrote a function called isEven that takes a number as a parameter and returns True if the
The number is even or False if it is odd. Use this function in a program that requests the user
the input of 10 numbers and then show, separately, the sum of all the even ones and the
sum of all the odd numbers.

Execution example:
Number: 620
12993
Number: 230
Number: 7
Number: 18
9234
Number: 38
Number: 567
Number: 8146
Number: 32
Sum of the pairs: 18318
Sum of the odd numbers: 13567

42
I wrote a function called sumatoriaDigitos that takes a number as a parameter and
return the sum of all its digits, reusing the strategy used in exercise 36.
Finally, write an algorithm that asks the user to enter several numbers until
enter the number 100, which will cut the repetition. For each number, show the sum of
its digits, for which the function sumatoriaDigitos will be called.

Execution example:
I wrote a number: 7124
Sum of the digits: 14
I wrote a number: 20
Sum of the digits: 2
I wrote a number: 916
Sum of the digits: 16
I wrote a number:100

Example code
lOMoARcPSD|28937740

def greaterOfTwo(x, y):


if x > y:
return x
else:
return y

def largestOfThree(x, y, z):


if greaterThanTwo(x,y)==x:
if greaterThanTwo(x,z)==x:
return x
else:
return z
else:
if greaterThanTwo(y,z)==y:
return y
else:
return z

n1=int(input("First number:"))
n2=int(input("Second number:"))
n3=int(input("Third number:"))
print(greatestOfThree(n1, n2, n3))

A program can have an unlimited number of functions and these can


be called from anywhere in the program, even from within others
functions. It is also possible to call the same function more than once, even
passing different data as arguments.
Each function has its own memory space, so the variables created
Within her, there are none within the rest of the program. This is why they could
using the same names for variables in different functions, but
will represent different data. Furthermore, although the value of the parameters
modify, this will not change the values of the arguments in the call to the
function. This is why it is often said that parameters are 'input' data.
and the return value is an 'output' data.

43
I wrote a program that allows the user to enter integer numbers. The repetition will end.
when the user enters a number for which the sum of its digits is greater than 1000
or multiple of 5. Finally, show how many odd numbers the user entered before
cut the repetition. Reuse the functions isPar and sumatoriaDigitos implemented in the
previous exercises.

Execution example:
I wrote a number: 16
I wrote a number: 922
I wrote a number: 1513
I wrote a number: 481
I wrote a number: 90
Cantidad de impares: 2
OMoARcPSD|28937740

44
I wrote a function that receives a string and returns True if it is a palindrome (that is, if it reads the same backwards).
same from left to right or from right to left), False otherwise. Use this
function in a program that allows the user to enter words until they enter the word
end
consistent). At the end, show the number of palindromes entered.

Execution example:
chain:abba
Chain:m
Chain: light
chain: recognize
Swallow chain
Chain: end
Cantidad de palíndromos: 3

Sample code
def countDigits(number):
cantidad=0
while number != 0:
quantity=quantity+1
number=number//10
return amount

-1
n=int(input("Positive number:"))
while cantidadDigitos(n)%3 != 0:
if n>greater:
n
n = int(input("Positive number:"))
print("Largest number entered:", largest)

Functions are also useful for thinking about programs in smaller parts.
small. In the example above, the repetition reads numbers until one is entered
one whose number of digits is a multiple of 3. When the repetition ends, of
all the entered numbers show which was the largest. But in order to build
the condition of this repetition is necessary to calculate the number of digits of a
number, which is delegated to a function that is called in each iteration, when
the condition is evaluated.

45
I wrote a program that allows the user to enter whole numbers until they enter one.
whose initial digit is 9 (which will not be processed). Once the repetition is finished, show
how many of the numbers that the user entered have only two divisors (for this it is possible
reuse part of the strategy developed in exercise 25).
lOMoARcPSD|28937740

Execution example:
Integer number: 167
Integer number: 11
Integer number: 821
Integer number: 38
Integer number: 292
Integer number: 3
Número entero:954
They have only 2 divisors: 4 numbers

You might also like