53 Python Exercises and Questions for Beginners
In this post, I’m going to list a bunch of Python exercises and questions for beginners. If you’re starting out with Python, this post is a good way to test your knowledge and learn new things. You may also want to check out my Python Tutorial for Beginners on YouTube and Python 3 Cheat Sheet.
NOTE: This post is a work in progress and I’ll regularly add new questions to the list. So, be sure to come back for new coding exercises. If you enjoy this post, please spread the love by share it with others.
Let’s get started!
Questions
Basics
- What is an expression?
- What is a syntax error?
- What is PEP8?
- What does a linter do?
- What is the result of this expression: “*” * 10
- What is CPython?
- How is CPython different from Jython?
- How is CPython different from IronPython?
Primitive Types
- What is a variable?
- What are the primitive built-in types in Python?
- When should we use “”” (tripe quotes) to define strings?
- Assuming (name = “John Smith”), what does name[1] return?
- What about name[-2]?
- What about name[1:-1]?
- How to get the length of name?
- What are the escape sequences in Python?
- What is the result of f“{2+2}+{10%3}”?
- Given (name = “john smith”), what will name.title() return?
- What does name.strip() do?
- What will name.find(“Smith”) return?
- What will be the value of name after we call name.replace(“j”, “k”)?
- How can we check to see if name contains “John”?
- What are the 3 types of numbers in Python?
Control Flow
- What is the difference between 10 / 3 and 10 // 3?
- What is the result of 10 ** 3?
- Given (x = 1), what will be the value of after we run (x += 2)?
- How can we round a number?
- What is the result of float(1)?
- What is the result of bool(“False”)?
- What are the falsy values in Python?
- What is the result of 10 == “10”?
- What is the result of “bag” > “apple”?
- What is the result of not(True or False)?
- Under what circumstances does the expression 18 <= age < 65 evaluate to True?
- What does range(1, 10, 2) return?
- Name 3 iterable objects in Python.
Functions
- What is the difference between a parameter and an argument?
- All functions in Python by default return …?
- What are keyword arguments and when should we use them?
- How can we make a parameter of a function optional?
- What happens when we prefix a parameter with an asterisk (*)?
- What about two asterisks (**)?
- What is scope?
- What is the difference between local and global variables?
- Why is using the global statement a bad practice?
Coding Exercises
- Write a function that returns the maximum of two numbers.
- Write a function called fizz_buzz that takes a number.
- If the number is divisible by 3, it should return “Fizz”.
- If it is divisible by 5, it should return “Buzz”.
- If it is divisible by both 3 and 5, it should return “FizzBuzz”.
- Otherwise, it should return the same number.
- Write a function for checking the speed of drivers. This function should have one parameter: speed.
- If speed is less than 70, it should print “Ok”.
- Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: “Points: 2”.
- If the driver gets more than 12 points, the function should print: “License suspended”
- Write a function called showNumbers that takes a parameter called limit. It should print all the numbers between 0 and limit with a label to identify the even and odd numbers. For example, if the limit is 3, it should print:
- 0 EVEN
- 1 ODD
- 2 EVEN
- 3 ODD
- Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
- Write a function called show_stars(rows). If rows is 5, it should print the following:
- *
- **
- ***
- ****
- *****
- Write a function that prints all the prime numbers between 0 and limit where limit is a parameter.
Want to learn all the Python skills you need? Check out my Complete Python Course for Beginners.
Instead of Python 3 Cheat Sheet its spelled out Python 3 Chat Sheet in the hyperlink on3rd line.
ALL THE PROGRAMS ARE VERY SIMPLE IN NATURE. A PROGRAM SHOULD BE EASY TO READ AND WRITE. DONT OVER-COMPLICATE THINGS AND USE PROPER LOGIC.
IF U HAVE ANY DOUBTS, SEND YOUR QUERIES AT ravuripraneeth2500@gmail.com (I AM A BEGINNER TOO!)
1). MAXIMUN OF 2 NUMBERS
def maximum(a,b):
if a>b:
print(a,”is the biggest number”)
else:
print(b,”is the biggest number”)
———-*–*–*–*–*———-
2).FIZZ BUZZ
def fb(num):
if num%3==0 and num%5==0:
print(“FizzBuzz”)
elif num%3==0:
print(“Fizz”)
elif num%5==0:
print(“Buzz”)
else:
print(num)
———-*–*–*–*–*———
3).SPEED
def speed(s):
d = 0
if s = 75 and s % 5 == 0:
d += ((s – 75) / 5) + 1
print(“Demrit points: “,d)
if d >= 12:
print(“Licanse is suspended”)
#You can use any value of speed
———-*–*–*–*–*———-
4).ODD EVEN
def showNumbers(limit):
for i in range(0,limit+1):
if i%2==0:
print(“EVEN”)
else:
print(“ODD”)
———-*–*–*–*–*———-
5).SUM OF MULTIPLES OF 3 AND 5
def num(limit):
sum = 0
for i in range(0,limit+1):
if i%3==0 or i%5==0:
sum +=i
print(sum)
———-*–*–*–*–*———
6). STAR PATTERN
def show_stars(rows):
for i in range(0,rows+1):
for j in range(0,i):
print(“*”,end = “”)
print(“\n”)
———-*–*–*–*–*———-
7).PRIME NUMBER
def prime(num):
if num==1 or num==0:
print(num,”is neither prime or composite”)
for i in range(2,num+1):
if num%i==0:
print(num,”is not a prime number”)
break
else:
print(num,”is a prime number”)
break
Prime no soln seems incorrect to me.Give 9 and see as input
For the 1st ques what if both the number are same! You should include if elif and else so that same number can be shown as both are same
the sixth solution can be simpler, as shown below:
def stars(rows):
temp =1
while(temp <= rows):
print('*' * temp)
temp = temp + 1
rows = int(input("Enter the number of rows: "))
stars(rows)
For the star pattern, here is the simplest code:
def show_stars(rows):
for I in range(1,rows+1):
print(“*” * I)
shows_stars(5)
Your PRIME NUMBER functions works but try to do it efficient.
Excercise no. 7
mine is likely simple-
def speed_limit(speed):
demerit_points = 0
if speed > 70:
demerit_points += (speed – 70) / 5
points = demerit_points
print(“Demerit Point: “, int(points))
if points > 12:
print(“License Suspended”)
elif speed <= 70:
print("Ok")
speed_limit(70)
POSTED THE WRONG PROGRAM FOR THE 7TH QUESTION. HERE IS THE CORRECT SOLUTION
def speed(s):
d = 0
if s=75 and s%5==0:
d+= ((s-75)/5)+1
print(“Demerit Points:”,d)
if d>=12:
print(“License is suspended”)
PLEASE USE PROPER INDENTATION. THE COMMENT BOX DOES NOT ACCEPT THE WHITESPACE AND I AM TOO LAZY TO GO OVER EACH PROGRAM. IF YOU THINK THERE IS A PROBLEM WITH THE PROGRAMS, MAIL ME AT ravuripraneeth2500@gmail.com
I have an even simpler one:
def check_speed(speed):
points = 70
point_value = speed – points
point = point_value / 5
if speed 12:
print(“License suspended”)
else:
print(f”Points: {int(point)}”)
[…] 53 Python Exercises and Questions for Beginners […]
[…] Python Exercises for Beginners: https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/ […]
[…] Python exercises for beginners: https://programmingwithmosh.com/python/python-exercises-and-questions-for-beginners/ […]
Hey Mosh. Could you please provide the answers to the practical questions?
This is the condition .no answer sheets provided by examiner u know!
Do you want the answers to the Coding Exercises?
yesss pleaseeee
def speed(s):
d = 0
s = int(s)
d = int(d)
if s 70 and s%5==0:
d += (s-70)/5
print(“Demerit points :” +str(d))
if d > 12:
print(“Licence suspended”)
speed(150)
# Solution 1
value1 = 30
value2 = 29
def maxi():
if value1 > value2:
print(“The maximum is: “, value1)
else:
print(“The maximum is: “, value2)
maxi()
# Solution 2
value = 90
def fizz_buzz(number):
nums = number
if nums % 3 == 0 and nums % 5 == 0:
print(“FizzBuzz”)
elif nums % 3 == 0:
print(“Fizz”)
elif nums % 5 == 0:
print(“Buzz”)
fizz_buzz(value)
# Solution 3
speed = 220
def speedometer(fast):
run = fast
if fast > 70:
exlimit = speed – 70
demerit = exlimit // 5
print(demerit)
if demerit > 12:
print(“License Suspended”)
else:
print(“Pay speeding charges”)
speedometer(speed)
# Solution 4
numbs = 20 # this is the limit
def showNumbers(limit):
for i in range(0, limit + 1):
if i % 2 == 0:
print(i, ” EVEN”)
else:
print(i, ” ODD”)
showNumbers(numbs)
# Solution 5
numz = 20 # this is the limit
def multisam(limit):
lamp = 0
lizo = limit
for i in range(0, lizo + 1):
if i % 3 == 0 or i % 5 == 0:
lamp = lamp + i
print(lamp)
multisam(numz)
# Solution 6
row = 5
def show_stars(rows):
for i in range(1, rows + 1):
for j in range(1, i):
print(“*”, end=””)
print(“\n”)
show_stars(row)
# Solution 7
lims = 15
def primetime(limit):
liz = limit
if liz == 1 and liz == 0:
print(“Not a prime number”)
for i in range(2, liz):
if i % 2 != 0:
print(i)
primetime(lims)
how to solve the question no .4 of code exercise?
class Number:
def shownum(self, num):
for no in range(int(num)):
if int(no) % 2 == 0:
print(f”{no} EVEN”)
else:
print(f”{no} ODD”)
n = input(“>”)
new = Number()
new.shownum(n)
#try above code, hope it will help you.
def showNumbers(limit):
for i in range(0, limit+1):
if i % 2 == 0:
even_or_odd = ” EVEN”
else:
even_or_odd = ” ODD”
print(str(i) + even_or_odd)
How should I enroll with this online course and get started to learn python..
Here’s the link to the course: https://codewithmosh.com/p/python-programming-course-beginners
Mosh plz help me I tried to enroll to your complete python course .
I tried to do the payment more than 10 times but the payment is not happening plz do something .I want to get your course .plzzz…
Hi mosh , can you please post the answers to the 53 questions.
please provide the answer sheet
hey mosh your tutorials are awesome…. Can you please provide tutorials on Django ?
I want to check my answers. Please provide the answer sheet!
I’m breaking my head with this exercise:
5. Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter). For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
I’ve wrote this code:
def sum_of_multiples(limit):
result = 0
limit += 1
for x in [3, 5]:
for y in range(1, limit):
if x*y <= limit:
result += x*y
return result
The problem is that I can't figure out how to eliminate de duplicates of the multiples of 3 and 5, like 15 or 30. Please help!
Sorry but de indentation disappears when I send the comment
did you get the answer
after a month not sure have u solved.
def three_five_sum(limit):
result = 0
for number in range(limit):
if number % 3 == 0:
result += number
elif number % 5 == 0:
result += number
return result
Hello! A way to do it is using a Set to add the numbers (a set can’t have an item duplicated), and then sum() to add all the elements in the set.
def add_num(limit):
k = 0
for l in range(limit):
if l % 3 == 0 or l % 5 == 0:
k += l
return k
print(add_num(20))
def sum_multiples(limit=20):
l = []
limit+=1
for i in xrange(limit):
if(i%3 == 0 or i%5 == 0):
l.append(i)
print(l)
sum_multiples()
Mmm… I like your approach to this problem.
to return the sum of the multiples you still need to sum them.
may be replace ‘print(l)’ with ‘print(sum(l))’
I just got the value with this logic. I used “not in” to check whether if the value already exists in the list or not:
I’m a very beginner with just 2-3 days of experience in coding. So plz don’t mind if this seems a lame logic. 🙂 But plz do let me know if there’s any way to enhance this. That would be really helpful for me to learn.
(Adding periods before each line just to keep the indentation in place if that might help.)
def sum_of_multiples(limit):
….count = 0
….sum = 0
….multiples = []
….while count <= limit:
……..if count % 3 == 0 and count not in multiples:
…………multiples.append(count)
…………print(count)
……..elif count % 5 == 0 and count not in multiples:
…………multiples.append(count)
…………print(count)
……..count+=1
….for each in multiples:
……..sum +=each
….print("\nSum is " + str(sum))
limit = int(input("Enter the limit: "))
sum_of_multiples(limit)
This is how i get there….
def multi_sum(limit):
_numbers=[0]
_multisum=[]
_while numbers[-1] != limit:
__numbers.append(numbers[-1] +1)
_for num in numbers:
__if num % 3 == 0 or num % 5 == 0:
___multisum.append(num)
_return multisum[1:]
print (multi_sum(25))
…did u meant something like this?
limit = int(input(‘limit: ‘))
lst3 =[x*3 for x in range(1, limit//3+1)]
lst5 = [x*5 for x in range(1, limit//5+1)]
lst = [*lst3, *lst5]
print(set(lst))
i liked this. just learning anyways
obviously missing the sum:
print(sum(set(lst)))
This is my logic might not be very efficient but it works
limit = int (input(‘Enter the limit: ‘))
m_three=1
m_five = 1
result = 0
for i in range(1, limit):
m_three = i * 3
m_five = i * 5
if m_five <= limit and m_three limit and m_three limit and m_three >limit:
break
print(result)
def multiple(r):
x=0
for x in range(r):
if x%3 == 0 and x%5 == 0:
print(x)
x+=1
You can eliminate duplicates from list e.g. like this:
mylist = [“a”, “b”, “a”, “c”, “c”]
mylist = list(dict.fromkeys(mylist))
print(mylist)
(source: https://www.w3schools.com/python/python_howto_remove_duplicates.asp)
My solution to the exercise:
def sumMultiples(n1, n2, limit):
return sum(list(dict.fromkeys(list(range(n1, limit+1, n1)) + list(range(n2, limit+1, n2)))))
print(sumMultiples(3, 5, 20))
list(range(…)) produces list in given range hence if you do
list(range(3,21,3)) you will get 3,6,9,12,15,18 and you can easily do the same with 5 and join the results, then get rid of duplicates with the method shown above
well try
def ….(limit):
for number in range(0, limit):
if number % 3 == 0 or number % 5 == 0:
x = number
x += number
print(x)
The answer in no: 5
def return_multiples(limit):
total = 0
for x in range(limit):
if x % 3 == 0 or x % 5 == 0:
total == x
total += x
print(total)
return_multiples(21)
def sum_multiple(item):
sum = 0
for i in range(0, item + 1):
if (i % 3 == 0) or (i % 5 == 0):
sum += i
return sum
print(sum_multiple(20))
L=[]
for i in range(0,100):
if i%3==0 and i%5==0:
L.append(i)
print(“The numbers are:”,L)
def sum_of_multiples(number):
sum=0
for a in (range(1,number+1)):
#print (a)
if (a%3==0 or a%5==0):
print(a)
sum +=a
print(sum)
sum_of_multiples(10)
have a if condition that removes the multiples 15
Here is the code:
2. FizzBuzz
def FizzBuzz(number):
if number % 3 == number % 5:
return ‘FizzBuzz’
elif number % 5 ==0:
return ‘Buzz’
elif number % 3 == 0:
return ‘Fizz’
else:
return number
number = int(input(‘>’))
drink = FizzBuzz(number)
print(drink)
3. Speed, License, Points
def lic_suspension (speed):
suspension_pt = 0
while suspension_pt != 12:
if speed = 12:
return ‘License Suspended’, suspension_pt
return “You’re overspeeding and license may get revoked”, suspension_pt
speed = int(input(‘<'))
vel, msg = lic_suspension(speed)
print(vel)
print(f"You've incurred {msg} points")
4. ODD EVEN
def showNumbers(limit):
coll_1 = {}
for item in range(limit+1):
if int(item) % 2 == 0:
num = item
type = "EVEN"
coll_1.update({num:type})
# num = "EVEN"
else:
num = item
type = "ODD"
coll_1.update({num:type})
return coll_1
limit = int(input("<"))
type = showNumbers(limit)
print(type)
5. Sum of Multiples of 3 and 5
def multiples (limit):
sum = 0
if limit <= 2:
print("Sum is zero. \nPlease enter a non zero number, preferrably greater than 3")
return sum
else:
for item in range(limit + 1):
if int(item) % 3 == 0 or int(item) % 5 == 0:
sum += int(item)
return sum
limit = int(input('<'))
total = multiples(limit)
print(f"Total sum of multiples of 3 and 5 till {limit} is {total}")
6. Stargazing
def show_start(limit):
for item in range(limit+1):
gaze = ""
for totem in range(item):
gaze += 'x'
print(gaze)
star = int(input("<"))
limit = show_start(star)
print("Isn't it a wonder?")
row = 0
limit = 5
while row < limit:
row += 1
times = int(row)
show_start = "*" * times
print(show_start)
For question 6,
def show_stars(row):
count = 0
for number in range(row):
count += 1
show = “*” * int(count)
print(show)
row = 5
show_stars(row)
def buzzfizz(number):
if number == 0:
return number
elif number / 3 == (round(number / 3)) and number / 5 == (round(number / 5)):
print(“Buzzfizz”)
elif number / 3 == (round(number / 3)):
print(“Buzz”)
elif number / 5 == (round(number / 5)):
print(“Fizz”)
else:
return number
print(buzzfizz(15))
# why does this return answer and “none”
Not sure. But for me this logic worked:
(Adding periods before each line just to keep the indentation in place if that might help.)
def fizz_buzz(num):
….if num%3 == 0 and num% 5 != 0:
……..print(“Fizz”)
….elif num % 3 != 0 and num % 5 == 0:
……..print(“Buzz”)
….elif num%3 == 0 and num% 5 == 0:
……..print(“FizzBuzz”)
….else:
……..print(num)
num = int(input(“Enter any number: “))
fizz_buzz(num)
2.
for number in range (1,101):
if number %3==0 and number %5==0:
print(‘Buzzfizz’)
elif number %3==0:
print(‘Buzz’)
elif number%5==0:
print(‘Fuzz’)
else:
print(number)
I’m not sure what logic to use for Exercise 7 to fetch prime numbers. Can someone give me a hint plz.
We’ll here are some my answers 🙂
2.
fizz_buzz = int(input(“Output: “))
if fizz_buzz % 3 == 0 and fizz_buzz % 5 != 0:
print(“Fizz”)
print(“You chose”, fizz_buzz)
elif fizz_buzz % 5 == 0 and fizz_buzz % 3 != 0:
print(“Buzz”)
print(“You chose”, fizz_buzz)
elif fizz_buzz % (5 and 3) == 0:
print(“FizzBuzz”)
print(“You chose”, fizz_buzz)
else:
print(fizz_buzz)
print(“You chose”, fizz_buzz)
5.
y = int(input(“”))
t = [3,5]
for i in list(range (1,y+1)):
if i % 3 == 0 or i % 5 == 0:
total = list(filter(lambda x:x % 3 == 0 or x % 5 == 0,range(1,y+1)))
print(total,”\nSum is”,sum(total))
7.
y = int(input(“”))
for i in range(1,y+1):
r = list(filter(lambda x:i % x== 0, range(1,y+1)))
if len(r) != 2:
print(i)
else:
print(i,”PRIME”)
I tried to make my codes as simple as possible, hope you like it!
def limit(lim):
x=0
for i in range(0,lim+1):
if i%3==0 or i%5==0:
x+=i
return x
limit(20)
print(limit(20))
Thanks for this! 🙂
# 3
def func(speed):
… if(speed12):
print(“license suspended”)
func(30)
ok
func(80)
point= 2.
func(100)
point= 6.0
func(200)
point= 26.0
license suspended
def func(speed):
… if(speed 12):
… print(“license suspended”)
…
>>> func(30)
ok
>>> func(80)
point= 2.0
>>> func(100)
point= 6.0
>>> func(200)
point= 26.0
license suspended
>>> func(135)
point= 13.0
license suspended
def shownumber(limit) :
for i in range(limit+1) :
if( i % 2!=0) :
print( i , ” :odd”)
else :
print(i , ” :even”)
>>> shownumber(6)
0 :even
1 :odd
2 :even
3 :odd
4 :even
5 :odd
6 :even
# 3
def func(speed) :
if(speed 12 ) :
print( ” license suspended”)
>>> func(30)
ok
>>> func(80)
point= 2.0
>>> func(100)
point= 6.0
>>> func(200)
point= 26.0
license suspended
>>> func(135)
point= 13.0
license suspended
Hi I am new to python and a total beginner. I am not clear on the above response of yours. Can you pls help
2. Write a function called Fizz_Buzz that takes a number :-
print(“———–> FIZZ BUZZ GAME <———–")
number = input("Enter The Number: ")
number = float(number)
if number % 15 == 0:
print("Fizz Buzz")
elif number % 3 == 0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)
4. Write a function called showNumbers that takes a parameter called limit. It should print all the numbers between 0 and limit with a label to identify the even and odd numbers. For example, if the limit is 3, it should print:
0 EVEN
1 ODD
2 EVEN
3 ODD
answer:-
print(“———–> EVEN ODD GAME {numbers} is Even”)
else:
print(f”-> {numbers} is odd”)
print(“———–> EVEN ODD GAME {numbers} is Even”)
else:
print(f”-> {numbers} is odd”)
answer is this
# Question 6
Limit = int(input(‘enter your number’)
l = ‘ ‘
for x in range(limit + 1):
x = x + 1
l = l + ‘*’
print(x, ‘—-‘ ,l)
speed =int(input(‘checking the speed of the driver: ‘))
def urSpeed(speed):
if speed speed >70:
print (‘point’, int((speed -70)/5) ,’is gone’)
if speed >=130:
print(‘Licence suspended’)
print(urSpeed(speed))
limit = int(input(‘give the limit’))
for x in range (0,(limit +1),1):
if x % 2==0 or x==0:
print(‘even’,x)
else:
print(‘odd’,x)
HELLO SIR. Please do you also post questions on c# .net? This could go a long way to help jump start beginners. Thanks in advance..
I did the Coding Exercises too successfully !
Question 1:
class Program:
def method(self):
number1 = int(input(print(“Enter first number = “)))
number2 = int(input(print(“Enter Second number = “)))
if number1 > number2:
return number1
else:
return number2
def main():
call = Program()
print(call.method())
if __name__ == ‘__main__’:
main()
Coding Exercises
Question : 2
class Progam:
def logic(self):
number = int(input(print(“Enter the number =”)))
if (number % 3) == 0:
return “Fizz”
elif (number % 5) == 0:
return “Buzz”
elif (number % 3) or (5 != 0):
return “FizzBuzz”
else:
return number
def main():
call = Progam()
print(call.logic())
if __name__ == ‘__main__’:
main()
a=input(“enter the range”)
s=0
s1=0
for i in range(1,a+1):
if(i%3==0):
c=i
s=s+c
print c,
elif(i%5==0):
b=i
s1=s1+b
print b,
m=s+s1
print ‘sum is \n’,m
def FizzBuzz(number):
if number % 3 == number % 5:
return “FizzBuzz”
elif number % 5 ==0:
return “Buzz”
elif number % 3 == 0:
return “Fizz”
else:
return number
number = int(input(“> “))
drink = FizzBuzz(number)
print(drink)
Sir can you send the python course material link?
I am new to python
Please let me know where i can the answers for the questions 🙂
i can’t download your sample data
Write a function for checking the speed of drivers. This function should have one parameter: speed.
If speed is less than 70, it should print “Ok”.
Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: “Points: 2”.
Hey Mosh. Its a cool tutorial but I can not find the excel file for the exercise. Is it supposed to be in the dropbox link? It doesn’t work, dropbox returns an error message.
I am looking for advance course in Python
Where is the answer for all the questions?
This is the answer for this execise: Write a function called showNumbers that takes a parameter called limit. It should print all the numbers between 0 and limit with a label to identify the even and odd numbers. For example, if the limit is 3, it should print:
def showNumbers(limit):
for x in range(limit + 1):
if x % 2:
print(str(x) + ” ODD”)
else:
print(str(x) + ” EVEN”)
showNumbers(3)
3.sum of multiples
def sumofmultiples(limit):
one = range(3,limit+1)
two = range(5,limit+1)
sum1 = 0
sum2 = 0
for item1 in one:
if item1 % 3==0:
sum1 += item1
for item2 in two:
if item2 % 5==0:
sum2 += item2
print(sum1+sum2)
limit = int(input(‘enter number’))
sum = sumofmultiples(limit)
sum
6.problem 6
def show_stars(rows):
for i in range(0,rows+1):
stars = ‘*’* i
print(stars)
rows = int(input(‘enter number of rows: ‘))
show = show_stars(rows)
show
note:- it was problem number 5 in the previous comment 🙂
Answer to function Question number 1
def max_number(a, b):
max = 0
if a > b:
max = a
return max
else:
max = b
return max
a = int(input(“Enter variable a: “))
b = int(input(“Enter variable b: “))
maximum = max_number(a, b)
print(maximum)
def shownumbers(limit):
a = “EVEN”
b = “ODD”
for i in range(limit):
if (i % 2) == 0:
print(f”{i} {a}”)
else:
print(f”{i} {b}”)
limit = int(input(“enter the limit”))
shownumbers(limit)
it’s correct but you need to have “for i in range(limit + 1):” instead of “for i in range(limit):” because it won’t match the output example in the question..
where can I get code for python video survillence
Hey Mosh, I just found your channel and I am loving it thank you. Would you do a video where you run through your or basic Python beginner questions? I love what you do because I need to get a feel for programming before I can run with it. Many of the courses I am taking on it ramp up so quickly I get lost and frustrated. If you went step by step through this site on your channel I would explode with awesomeness! https://www.w3resource.com/python-exercises/python-functions-exercises.php
Answer of Excercise Number 2:
***The Fizzbuzz Function***
Please reply and tell me if my code is correct or not. Im beginner.
i dont have that much knowledge.
def fizzbuzz():
a= int(input(“Enter Number: “))
if a%3==0:
return (“Fizz !!!”)
elif a%5==0:
return (“Buzz !!!”)
elif a%3==0 and a%5==0:
return (“Fizz Buzz”)
else:
return a
print(fizzbuzz())
Answer of Exercise no. 1:
a= int(input(“Enter 1st Number: “))
b= int(input(“Enter 2nd Number: “))
def maxn(a,b):
if a>=b:
return (a,”Is Greater”)
else:
return (“Is Greater”)
print(maxn(a,b))
4.
def number(limit):
for i in range(limit + 1):
if(i%2==0):
print(i,”: Even “)
else:
print(i, “: Odd”)
enter = number(int(input(“Enter limit: “)))
6.
x = int(input(“Enter the number: “))
for i in range(0, x):
for j in range(0,i+1):
print(“*” , end= “”)
print(“\n”)
7.
start = int(input(“ENter Your Start Number: “))
end = int(input(“Enter your Last Number: “))
print(“Prime numbers between “, start , end)
for i in range( start , end + 1):
if i > 1:
for j in range(2, i):
if(i % j) == 0:
break
else:
print(i)
fizz_buzz= int(input(‘Enter a number ‘) )
if (fizz_buzz)%3==0 and (fizz_buzz)%5==0:
print(‘Fizzbuzz’)
elif (fizz_buzz)% 3==0:
print(‘Fizz’)
elif (fizz_buzz)%5==0:
print(‘Buzz’)
else:
print(fizz_buzz)
fizz_buzz question:
def fizz_bizz(numbers):
if number % 3 == 0 and number % 5 == 0:
print(“FizzBuzz”)
elif number % 3 == 0:
print(“Fizz”)
elif number % 5 == 0:
print(“Buzz”)
number = int(input(“Number: “))
fizz_bizz(numbers= number)
Speed Drivers problem:
speed = int(input(“Speed = “))
def limit(speed_calculator):
if speed 12:
print(“License suspended”)
limit(speed_calculator= speed)
Speed Drivers problem:
speed = int(input(“Speed = “))
def limit(speed_calculator):
if speed 12:
print(“License suspended”)
limit(speed_calculator= speed)
2. def fizz_buzz():
n = int(input(“input the number: “))
if n%3==0and n%5==0:
print(“fizz_buzz”)
elif n%3==0:
print(“fizz”)
elif n%5==0:
print(“buzz”)
else:
print(n)
fizz_buzz()
3. def Chkspeed():
speed = int(input(“Enter driver speed: “))
if speed = 5:
demPoints = demPoints + 1
if demPoints >= 12:
print(“License cancel”)
else:
print(“Points:”, demPoints)
Chkspeed()
4. def showNumbers():
limit = int(input(“Enter the limit: “))
for x in range(limit+1):
if x % 2 == 0:
print(x, “EVEN”)
else:
print(x, “ODD”)
showNumbers()
5. def SumOfMultiples():
limit = int(input(“Enter the limit: “))
sum = 0
for x in range(limit+1):
if x % 3 == 0 or x % 5 == 0:
sum = sum + x
print(sum)
SumOfMultiples()
def showStars():
rows = int(input(“Enter the number of rows: “))
for i in range(rows):
print(“*” * (i + 1), ” ” * (rows – i – 1))
showStars()
1. exercise:
def max_number(first, second):
if first > second:
return first
else:
return second
first = int(input(“Type your first number: “))
second = int(input(“Type your second number: “))
biggest_number = max_number(first, second)
print(biggest_number)
My answer for coding exercise #7:
print(“*** EXERCISE #7 ***”)
def primes(limit):
prime_numbers = []
for number in range(2, limit+1): # 2 is smallest prime number
score = 0
for test in range(2, number):
reminder = number % test
if reminder == 0:
score = 1
if score == 0:
prime_numbers.append(number)
print(prime_numbers)
primes(100)
added “_” to simulate tabs in order to make the code easier to understand and simpflied a litte bit
def primes(limit):
___prime_numbers = []
___for number in range(2, limit+1): # 2 is smallest prime number
______score = 0
______for test in range(2, number): # 1 must be excluded
_________if number % test == 0:
____________score = 1
______if score == 0:
______prime_numbers.append(number)
___print(prime_numbers)
primes(100)
Syntax error is when the compiler not understand a line coad
Variable is used to store a value
I just started learning python and it is my 5th code was so much fun.
fizz_buzz = int(input(“enter any no: “))
if(fizz_buzz %3 == 0 and fizz_buzz %5 == 0):
print(‘FizzBuzz’)
elif fizz_buzz %3 == 0:
print(‘Fizz’)
elif fizz_buzz %5 == 0:
print(‘Buzz’)
else:
print(fizz_buzz)
Hi Mosh and everyone, I am a Filipino who is trying to learn Python for 4 days now by watching Mosh’s Youtube Videos. Suddenly out of curiosity, I tried searching for python exercises to assess my skills in python programming. Fortunately I was able to solve all of the 7 problems posted in this page. The link of .py file that I have created for all the solutions is available below for everyone who is interested to learn the solutions. Thank you very much Mosh for this great forum and you may keep on posting and sharing your talent for all the aspiring future programmers in the world. Keep up the good work. Thank you!
LINK SOLUTION: https://drive.google.com/open?id=102SnZY6NgwapZI-_BflfKjCzlym1vLEh
def showNumber(limit):
for number in range(limit + 1 ):
if number % 2==0:
print(number, ‘EVEN’)
else:
print(number, ‘ODD’)
3 speed checking
speed= int(input(‘enter your speed ‘))
points = (speed- 70)/5
if (speed- 70)/5 0:
for x in range(0,11):
if int(points) == x:
print(‘{} points gone’.format(x+1))
if (speed- 70)/5 > 11:
print(‘Total points 12 are gone!!\nDriver licence is gone’)
speed = int(input(“provera brzine :”))
points = (speed – 70)/5
if speed = 130:
print(“licensa suspendovana”)
for x in range(0, 11):
if int(points) == x:
print(“{} points “.format(x))
Thank you so much Mosh. Good exercises on python for beginners. Waiting for more advanced topics in python.
hey mosh thanks for you Youtube tutorial i have learnt python and, now i have decided to be a software engineer at google inspired by you .I am a 9 th grader from India
Ans1:
def sum(a,b):
if a > b:
print(a)
else:
print(b)
a=input(‘input 1st no:’)
b=input(‘input 2nd no:’)
sum(a,b)
Ans2:
def fizz_buzz(number):
if number % 3 == 0 and not number%5==0:
print(‘Fizz’)
elif number % 5 == 0 and not number%3==0:
print(‘Buzz’)
elif number % 5 == 0 and number % 3 == 0:
print(‘FizzBuzz’)
else:
print(number)
number=int(input(‘Input the number:’))
fizz_buzz(number)
Ans3:
def check_speed(speed):
if speed 12:
print(‘Licence Suspended’)
speed=int(input(‘speed::’))
check_speed(speed)
Ans4:
def showNumbers(limit):
for item in c:
if item % 2 == 0:
print(‘{} EVEN’.format(item))
else:
print(‘{} ODD’.format(item))
limit = int(input(‘input no::’))
c=range(limit+1)
showNumbers(limit)
Ans5:
def sum_of_no(number):
sum = 0
for item in c:
if item % 3 == 0 or item % 5 == 0:
sum = sum + item
print(sum)
number=int(input(‘input the number::’))
c=range(1,number+1)
sum_of_no(number)
Ans6:
def show_stars(rows):
c = range(1, rows + 1)
for item in c:
print(item * ‘*’)
rows=int(input(‘rows::’))
show_stars(rows)
Ans7:
def prime_numbers(lower,upper):
for item in range(lower, upper):
if item > 1:
for i in range(2, item):
if item % i == 0:
break
else:
print(item)
lower=int(input(‘lower range’))
upper=int(input(‘upper range’))
prime_numbers(lower,upper)
Hi Mosh! These exercises you gave were very beneficial. Can you give more of the coding exercises
Here comes my answers to the 53 python exercise: I will only post the coding exercises:
#Answers to “coding exercise” from Mosh
# question nr 1
def max_nr(number1, number2):
if number1 > number2:
return number1
elif number2 > number1:
return number2
else:
return “Number are the same”
print(max_nr(4, 3))
# question nr 2
def fizz_buzz(input):
if (input % 5 == 0) and (input % 3 == 0):
return “fizzbuzz”
if input % 3 == 0:
return “fizz”
if input % 5 == 0:
return “buzz”
return input
print(fizz_buzz(2))
# question nr 3
def driver_speed(speed):
suspention_point = 0
if speed = 70:
suspention_point += ((speed-70)/5)
print(“points:”, suspention_point)
if suspention_point >= 12:
print(“license is suspended”)
driver_speed(130)
# question nr 4
def show_numbers(limit):
number = 0
while number <= limit:
if (number % 2 == 0):
print(number, "Even")
else:
print(number, "Odd")
number += 1
show_numbers(3)
# question nr 5
def show_multiple(limit):
number = 1
while number <= limit:
if (number % 3 == 0):
print(number)
elif (number % 5 == 0):
print(number)
number += 1
show_multiple(20)
# question nr 6
def show_stars(rows):
for i in range(rows):
for j in range(i+1):
print("*", end="")
print()
show_stars(5)
# question nr 7 ## WISHING SOMEONE TO EXPLAIN THIS FOR ME
def prime(limit):
if limit <= 1:
return False
for i in range(2, limit):
if limit % i == 0:
return False
return True
print(prime(9))
OBS!!!!!!
Someone who can explain to me question nr 7. I was not able to print all the prime numbers at once, but I was able show whether a given number is prime or not
speed of Driver Exercise solution:
point = 0
while True:
speed = int(input(‘Enter your speed: ‘))
if speed 70 and speed % 5 == 0:
point = (speed – 70)//5
print(“Point”, point)
if point >= 12:
print(‘Licence suspended’)
break
Speed of Driver code as function:
def speedLimit(speed):
if speed 70 and speed % 5 == 0:
point = (speed – 70) // 5
print(“Point”, point)
if point >= 12:
print(‘Licence suspended’)
# Give speedLimit argument
speedLimit(80)
Helpful Questions for a quick practice. I am always looking forward to connect with such sites. Will surely check out your courses. They seem quite useful.
My solution for exercise 5:
====================
def sum_of_limit(limit):
summing = 0
multiples = 0
for summing in range(limit + 1):
if summing % 3 == 0 or summing % 5 == 0:
multiples = multiples + summing
print(f’Sum of multiples of 3 and 5 is: {multiples}’)
used_limit = int(input())
sum_of_limit(used_limit)
My solution for exercise 6:
====================
def show_stars(rows):
for i in range(rows + 1):
print(“*” * i)
selected_rows = int(input())
show_stars(selected_rows)
#Ques:1
”’Write a function that returns the maximum of two numbers.
def maxOfTwo(n,m):
return max(n,m)
n1=int(input(‘enter number 1 : ‘))
n2=int(input(‘enter number 2 : ‘))
print(maxOfTwo(n1,n2)) ”’
#Ques:2
”’
Write a function called fizz_buzz that takes a number.
If the number is divisible by 3, it should return “Fizz”.
If it is divisible by 5, it should return “Buzz”.
If it is divisible by both 3 and 5, it should return “FizzBuzz”.
Otherwise, it should return the same number.
def fizz(n):
if n%3==0 and n%5!=0:
return ‘Fizz’
elif n%5==0 and n%3!=0:
return ‘Buzz’
elif n%3==0 and n%5==0:
return ‘FizzBuzz’
else:
return n
num=int(input(“enter number”))
print(fizz(num))
”’
#Ques:3
”’Write a function for checking the speed of drivers.
This function should have one parameter: speed.
If speed is less than 70, it should print “Ok”.
Otherwise, for every 5km above the speed limit (70),
it should give the driver one demerit point
and print the total number of demerit points.
For example, if the speed is 80, it should print: “Points: 2”.
If the driver gets more than 12 points, the function
should print: “License suspended”
def speedCheck(s):
a=70
b=5
points=0
if s<=0:
print('Speed is Zero')
elif s0:
print(‘Ok’)
if s>a:
c=(s-70)//b
points=points+c
print(‘points: ‘,points)
if points>12:
print(‘Licence Suspended’)
speed=int(input(‘what is your speed: ‘))
speedCheck(speed)
”’
#Ques:4
”’Write a function called showNumbers that takes a parameter called limit.
It should print all the numbers between 0 and limit with a label to identify
the even and odd numbers. For example, if the limit is 3, it should print:
0 EVEN
1 ODD
2 EVEN
3 ODD
def showNumbers(limit):
for i in range(0, limit+1):
if i%2==0:
print(i,’EVEN’)
else:
print(i,’ODD’)
val=int(input(‘enter the number: ‘))
showNumbers(val)
”’
#Ques:5
”’Write a function that returns the sum of multiples of 3 and 5
between 0 and limit (parameter). For example, if limit is 20,
it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
def sumMul(limit):
l1=0
if limit>0:
for i in range(0, limit+1):
if i%3==0 or i%5==0:
l1+=i
print(‘Sum is : ‘, l1)
limit=int(input(‘enter the value: ‘))
sumMul(limit)
”’
#Ques:6
”’
*
**
***
****
*****
”’
def show_start(rows):
for i in range(rows):
print()
for j in range(i+1):
print(‘*’, end=”)
rows=int (input(‘enter num: ‘))
show_start(rows)
solution to coding exercised 3(modified as speed 71-75 = 1 dp, 76-80 = 2dp and so on)
def speed(s):
if s <= 70:
print('ok')
else:
s = s + (5- (s % 5))
dp = (s – 70) // 5
if dp < 12:
print('Demerits point: ', str(dp))
else:
print("Licence Suspended")
def main():
s = int(input('Please enter your speed: '))
speed(s)
main()
the best result for line 4 is this
n = 4
for awesome,x in enumerate(range(n)):
if x%2==0:
print(f”{awesome} even”)
elif x%2==1:
print(f”{awesome} odd”)
###########
Write a function called fizz_buzz that takes a number.
If the number is divisible by 3, it should return “Fizz”.
If it is divisible by 5, it should return “Buzz”.
If it is divisible by both 3 and 5, it should return “FizzBuzz”.
Otherwise, it should return the same number.
fizz_bazz = input()
x = int(3)
m = int(5)
if (int(fizz_bazz) % int(x) == 0) & (int(fizz_bazz) % int(m) == 0):
print(“FizzBuzz”)
elif int(fizz_bazz) % int(x) == 0:
print(“fizz”)
elif int(fizz_bazz) % int(m) == 0:
print(“Buzz”)
else:
print(fizz_bazz)
#########
Write a function for checking the speed of drivers. This function should have one parameter: speed.
If speed is less than 70, it should print “Ok”.
Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points.
For example, if the speed is 80, it should print: “Points: 2”.
If the driver gets more than 12 points, the function should print: “License suspended”
speed = input()
normal = 70
mx = 130
pt = int(speed) – int(normal)
demerit = int(pt) // 5
if int(speed) int(mx):
print(“License suspended”)
print(“🚗 ♐ Extra Speed”, pt, ‘Km ❗❗❗❗ & Points:->’, demerit)
elif int(speed) > int(normal):
print(“Keep Speed In Control”)
print(“🚗 ♐ Extra Speed”, pt, ‘Km ❗❗❗❗ & Points:->’, demerit)
question 3; (easy)
def check_speed(speed):
speed_limit = 70
value = (speed – speed_limit)
demerit_points = value/5
if speed < 70:
print('OK')
elif demerit_points <= 12:
print(f'Points: {int(demerit_points)}')
else:
print('License suspended')
s = int(input('speed = '))
check_speed(s)
2)
def fizz_buzz(num):
if num %3 ==0 and num % 5 ==0:
print(‘FizzBuzz’)
elif num %5 == 0 :
print ( ‘Buzz’)
elif num %3 ==0:
print(‘Fizz’)
else:
print(num)
numb = int(input(‘enter the number = ‘))
fizz_buzz(numb)
3)
def check_speed(speed):
speed_limit = 70
value = (speed – speed_limit)
demerit_points = value/5
if speed < 70:
print('OK')
elif demerit_points <= 12:
print(f'Points: {int(demerit_points)}')
else:
print('License suspended')
s = int(input('speed = '))
check_speed(s)
4)
def showNos(limt):
for item in range (0,limt+1):
if item % 2 == 0:
o_e = 'EVEN'
else:
o_e = 'ODD'
print(f'{item} {o_e}')
l = int(input('enter limit = '))
showNos(l)
5)
def sum_multp(limit):
summ = 0
for item in range(0,limit+1):
if item % 3 ==0 or item % 5 ==0:
summ += item
print(summ)
l = int(input('enter limit = '))
sum_multp(l)
6)
def show_stars(rows):
for item in range(rows+1):
print(item * '*')
row_no = int(input('print row number = '))
show_stars(row_no)
7)
def prime_no(limit):
for item in range(limit+1):
if item %2 == 0:
print(item)
no = int(input('enter limit = '))
prime_no(no)
1:
def maximum(num1,num2):
if num1>=num2:
return num1
if num2>num1:
return num2
print(maximum(15,75))
print(“”)
2:
def fizz_buzz(num):
if num % 3 == 0 and num % 5 != 0:
return “Fizz”
elif num % 5 == 0 and num % 3 != 0:
return “Buzz”
elif num % 5 == 0 and num % 3 == 0:
return “FizzBuzz”
else:
return num
print(fizz_buzz(15))
print(“”)
3:
def speed_test(speed):
demerit = 0
if speed <= 70:
return "Speed is OK!"
else:
for i in range(70,speed,5):
demerit += 1
if demerit <= 12:
return f"Points: {demerit}"
else:
return "License Suspended!!"
print(speed_test(70))
print("")
4:
def show_numbers(limit):
loop = 0
for i in range(0,limit,1):
if loop == 0:
print(f"{i} EVEN")
loop = 1
else:
print(f"{i} ODD")
loop = 0
show_numbers(4)
print("")
5:
def sum_multiples(limit):
sum = 0
for i in range(0,limit+1,1):
if i % 3 == 0 or i % 5 == 0:
sum += i
return sum
print(sum_multiples(20))
print("")
6.
def show_stars(rows):
for i in range(1,rows+1):
print('*' * i)
show_stars(5)
print("")
7:
def prime_numbers(limit):
prime = True
for i in range(1,limit+1,1):
prime = True
for y in range(2,i,1):
if i % y == 0:
prime = False
if prime:
print(i)
prime_numbers(100)
print("")
Thank you so much Mosh for making learning easy and fun. Much respect and stay healthy.
q1)x=int(input(“enter your first number”))
y=int(input(“enter your second number”))
def maximum(x,y):
if x > y:
return (str(x) + ” is greater than “+str(y))
else:
return(str(y) +” is greater than “+ str(x))
print(maximum(x,y))
q2)n1 = int(input(“Enter your number”))
div3 = n1 % 3
div5 = n1 % 5
div = n1 % 15
if div3==0:
print(“Fizz”)
if div5==0:
print(“buzz”)
if div==0:
print(‘fizzbuzz’)
else:
print(n1)
q3)speed=int(input(“enter the speed”))
demerit=int((speed-70) / 5)
def speedofdrivers(speed):
if speed 12:
print(“License suspensded”)
I am a beginner and tried my best to solve these questions. These may not be the best codes but i guess they still solve the problem.
while True:
speed = int(input(“car speed: “))
def limit(speed):
point = 0
if speed 70:
point += ((speed – 75)//5)+1
print(point)
if point >= 12:
print(“Licanse is suspended”)
limit(speed)
def fizz_bizz:
num=int(input(num: ))
if num%3=0 and num%5=0:
print(“fizz bizz”)
elif num%3=0 and not num%5=0:
print(‘fizz’)
elif num%5=0 and not num%3=0:
print(“bizz”)
else :
print(“num is not a mul of 3 or 5”)
Exercise 2:
— CODE
number = int(input(“Enter: “))
def fizz_buzz(number):
if number%3==0 and number%5==0:
print(“FizzBuzz”)
elif number%3==0:
print(“Fizz”)
elif number%5==0:
print(“Buzz”)
else:
print(number)
fizz_buzz(number)
— OUTPUT
if it is divisible by both 3 and 5, it should return “FizzBuzz”:
Enter: 15
FizzBuzz
if it is divisible by 5, it should return “Buzz”:
Enter: 25
Buzz
if the number is divisible by 3, it should return “Fizz”:
Enter: 9
Fizz
otherwise, it should return the same number:
Enter: 179
179
# —————— 01 ——————
# Write a function that returns the maximum of two numbers.
# for more numbers
def max_num(numbers):
max = numbers[0]
for number in numbers:
if number > max:
max = number
pass
return max
# for two numbers
def maximum(a,b):
if a>b:
print(f'{a} is the biggest number’)
else:
print(f'{b} is the biggest number’)
# numbers = [22, 5, 8, 12, 15, 33, 0, 1, 2, 5]
# x = max_num(numbers)
# print(x)
# —————— 02 ——————
# Write a function called fizz_buzz that takes a number.
# • If the number is divisible by 3, it should return “Fizz”.
# • If it is divisible by 5, it should return “Buzz”.
# • If it is divisible by both 3 and 5, it should return “FizzBuzz”.
# • Otherwise, it should return the same number.
def fizz_buzz(number):
if number % 3 == 0:
if number % 5 == 0:
print(“FizzBuzz”)
else:
print(“Fizz”)
elif number % 5 == 0:
print(“Buzz”)
else:
print(number)
return number
# fizz_buzz(3)
# fizz_buzz(15)
# fizz_buzz(168)
# fizz_buzz(10)
# fizz_buzz(251684)
# —————— 03 ——————
# Write a function for checking the speed of drivers. This function should have one parameter: speed.
# If speed is less than 70, it should print “Ok”.
# Otherwise, for every 5km above the speed limit (70),
# it should give the driver one demerit point and print the total number of demerit points.
# For example, if the speed is 80, it should print: “Points: 2”.
# If the driver gets more than 12 points, the function should print: “License suspended”
def speed_check(speed):
if speed < 70:
print("Ok")
else:
point = (speed – 70) // 5
print(f'Points: {point}.')
return speed
# speed_check(70)
# speed_check(85)
# speed_check(107)
# —————— 04 ——————
# Write a function called showNumbers that takes a parameter called limit.
# It should print all the numbers between 0 and limit with a label to identify the even and odd numbers.
# For example, if the limit is 3, it should print:
# 0 EVEN
# 1 ODD
# 2 EVEN
# 3 ODD
def showNumbers(limit):
for number in range(limit + 1):
if number % 2 == 0:
print(f'{number} EVEN')
else:
print(f'{number} ODD')
return limit
# showNumbers(10)
# —————— 05 ——————
# Write a function that returns the sum of multiples of 3 and 5 between 0 and limit (parameter).
# For example, if limit is 20, it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
def sum_of_multiples(limit):
x = 0
for number in range(limit + 1):
if number % 3 == 0 or number % 5 == 0:
x += number
return x
# y = sum_of_multiples(20)
# print(y)
# —————— 06 ——————
# Write a function called show_stars(rows). If rows is 5, it should print the following:
# *
# **
# ***
# ****
# *****
def show_stars(row):
x = ''
for i in range(row):
x += '*'
print(x)
# show_stars(2)
# show_stars(5)
# —————— 07 ——————
def prime_numbers(limit):
prime = True
for i in range(2,limit):
prime = True
for y in range(2,i):
if i % y ==0:
prime = False
if prime:
print(i)
# prime_numbers(10)
prime_numbers(22)
def speed_check(speed):
demerit_point = 0
while demerit_point < 12:
if speed <= 70:
print("Ok")
elif speed % 5 == 0:
demerit_point += 1
print(f"Point: {demerit_point}")
speed = int(input("current speed: "))
print("License suspended!")
speed_check(70)
def speed_check(speed):
demerit_point = 0
while demerit_point < 12:
if speed <= 70:
print("Ok")
elif speed % 5 == 0:
demerit_point += 1
print(f"Point: {demerit_point}")
speed = int(input("current speed: "))
print("License suspended!")
speed_check(70)
Pls go through and add the indentations as supposed. The program keeps responding once you initially enter the first speed check (70). I think there are other solutions with shorter lines of code, but as a beginner this is my solution
# CHECK YOUR SPEED 8-LINE SOLUTION
speed = int(input(‘Enter speed (km) ‘))
points = int(((speed – 1) – 70) / 5) + 1
if 0 < speed <= 70:
print('Ok')
elif 70 < speed < 130:
print(f'Too fast! You have {points} point(s)')
elif speed == 0:
print("Don't waste a cop's time!") # 2 extra lines just for fun 😉
else:
print('More than 12 points!! License suspended')
# def maximum(num1,num2):
# return max(num1,num2)
# print(maximum(2,7))
# def fizz_buzz(num):
# if num%3==0 and num%5 ==0:
# result = ‘FizzBuzz’
# elif num%3 == 0:
# result = ‘Fizz’
# elif num%5 == 0:
# result = ‘Buzz’
# else:
# result = num
# return result
# print(fizz_buzz(5))
# def speed_checker(speed,):
# if speed == 70:
# result = ‘Ok’
# elif speed > 70:
# point = (speed-70)/5
# result = f’points: {point}’
# if point == 12:
# # result = “license suspended”
# result = f’points: {point}, license suspended’
# return result
# print(speed_checker(112))
# def showNumber(limit):
# for i in [f”Even {i}” if i%2==0 else f”Odd {i}” for i in range(limit+1)]:
# print(i)
# showNumber(3)
def multiples(limit):
number = ”
for i in range(1,limit+1):
if i%3==0 or i %5==0:
number += str(i)+’, ‘
return number
print(multiples(20))
—————— <>—————————————
def Even_odd(limit):
for i in range(0,limit+1):
even_odd = {}
if i %2 == 0:
even_odd[‘even’] = i
else:
even_odd[‘odd’] = i
for key,value in even_odd.items():
print(f'{value} {key}’)
Even_odd(4)
_______________@@@@@@_________________________
mosh u r incrediable man. Thanks for the free tutorial
Here are my solutions:
#Function to return max of two numbers
”’def max(a,b):
if a>b:
return a
else:
return b
a=eval(input(“Enter the 1st number:”))
b=eval(input(“Enter the 2nd Number:”))
print(max(a,b))”’
#Write a function called fizz_buzz that takes a number.
#If the number is divisible by 3, it should return “Fizz”.
#If it is divisible by 5, it should return “Buzz”.
#If it is divisible by both 3 and 5, it should return “FizzBuzz”.
#Otherwise, it should return the same number.
”’def fizz_buzz(a):
if a%3==0 and a%5==0:
return “FizzBuzz”
elif a%3==0:
return “Fizz”
elif a%5==0:
return “Buzz”
else:
return a
a=eval(input(“Enter the Number:”))
print(fizz_buzz(a))”’
#Write a function for checking the speed of drivers. This function should have one parameter: speed.
#If speed is less than 70, it should print “Ok”.
#Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points.
#For example, if the speed is 80, it should print: “Points: 2”.
#If the driver gets more than 12 points, the function should print: “License suspended”
”’def driver_speed(speed):
if speed70 and speed%5==0:
d+=((speed-75)/5)+1
print(“Points:”,d)
if d>=12:
print(“Licence Suspended”)
speed=eval(input(“Enter the speed:”))
driver_speed(speed)”’
#Write a function called showNumbers that takes a parameter called limit.
#It should print all the numbers between 0 and limit
#with a label to identify the even and odd numbers.
#For example, if the limit is 3, it should print: 0 EVEN 1 ODD 2 EVEN 3 ODD
”’def showNumbers(limit):
for i in range(0,limit+1):
if i%2==0:
print(i,”EVEN”)
else:
print(i,”ODD”)
limit=eval(input(“Enter the Number:”))
showNumbers(limit)”’
#Write a function that returns the sum of multiples of 3 and 5 between 0 and
#limit (parameter). For example, if limit is 20,
#it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
”’def func(limit):
for i in range(0,limit+1):
if i%3==0 or i%5==0:
lst.append(i)
return sum(lst)
lst=[]
limit=eval(input(“Enter the number:”))
print(func(limit))”’
#Write a function called show_stars(rows). If rows is 5, it should print the following:
#*
#**
#***
#****
#*****
”’def show_stars(rows):
for i in range(rows+1):
for j in range(i):
print(“*”,end=””)
print(” “)
rows=eval(input(“Enter the number:”))
show_stars(rows)”’
#Write a function that prints all the prime numbers
#between 0 and limit where limit is a parameter.
def prime(limit):
for i in range(1,limit):
for j in range(2,i):
if i%j==0:
break
else:
print(i,”Prime”)
limit=eval(input(“Enter the Number:”))
prime(limit)
#Function to return max of two numbers
”’def max(a,b):
if a>b:
return a
else:
return b
a=eval(input(“Enter the 1st number:”))
b=eval(input(“Enter the 2nd Number:”))
print(max(a,b))”’
#Write a function called fizz_buzz that takes a number.
#If the number is divisible by 3, it should return “Fizz”.
#If it is divisible by 5, it should return “Buzz”.
#If it is divisible by both 3 and 5, it should return “FizzBuzz”.
#Otherwise, it should return the same number.
”’def fizz_buzz(a):
if a%3==0 and a%5==0:
return “FizzBuzz”
elif a%3==0:
return “Fizz”
elif a%5==0:
return “Buzz”
else:
return a
a=eval(input(“Enter the Number:”))
print(fizz_buzz(a))”’
#Write a function for checking the speed of drivers. This function should have one parameter: speed.
#If speed is less than 70, it should print “Ok”.
#Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points.
#For example, if the speed is 80, it should print: “Points: 2”.
#If the driver gets more than 12 points, the function should print: “License suspended”
”’def driver_speed(speed):
if speed70 and speed%5==0:
d+=((speed-75)/5)+1
print(“Points:”,d)
if d>=12:
print(“Licence Suspended”)
speed=eval(input(“Enter the speed:”))
driver_speed(speed)”’
#Write a function called showNumbers that takes a parameter called limit.
#It should print all the numbers between 0 and limit
#with a label to identify the even and odd numbers.
#For example, if the limit is 3, it should print: 0 EVEN 1 ODD 2 EVEN 3 ODD
”’def showNumbers(limit):
for i in range(0,limit+1):
if i%2==0:
print(i,”EVEN”)
else:
print(i,”ODD”)
limit=eval(input(“Enter the Number:”))
showNumbers(limit)”’
#Write a function that returns the sum of multiples of 3 and 5 between 0 and
#limit (parameter). For example, if limit is 20,
#it should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
”’def func(limit):
for i in range(0,limit+1):
if i%3==0 or i%5==0:
lst.append(i)
return sum(lst)
lst=[]
limit=eval(input(“Enter the number:”))
print(func(limit))”’
#Write a function called show_stars(rows). If rows is 5, it should print the following:
#*
#**
#***
#****
#*****
”’def show_stars(rows):
for i in range(rows+1):
for j in range(i):
print(“*”,end=””)
print(” “)
rows=eval(input(“Enter the number:”))
show_stars(rows)”’
#Write a function that prints all the prime numbers
#between 0 and limit where limit is a parameter.
def prime(limit):
for i in range(1,limit):
for j in range(2,i):
if i%j==0:
break
else:
print(i,”Prime”)
limit=eval(input(“Enter the Number:”))
prime(limit)
def claculation(limit):
sum=0
for i in range(0,21):
if i%3 == 0 or i%5 == 0:
sum=sum+i
return sum
sumation=claculation(range(0,21))
print(sumation)
def max_of_numbers(a,b):
result = input(max(a,b))
return result
max_of_numbers(12,34)
Thanks for the hard work
# second question answer
number = int(input(“Enter number: “))
def fizz_buzz(number):
while number:
if (number % 3 == 0 and number % 5 == 0):
print(“FizzBuzz”)
elif (number % 5 == 0):
print(“Buzz”)
elif (number % 3 == 0):
print(“Fizz”)
else:
print(number)
break
fizz_buzz(number)
speed = int(input(“Enter speed: “))
suspend_point = 0
if speed = 75:
point = ((speed-75)/5)+1
suspend_point += point
if suspend_point>1:
print(f”your license may be suspended you got {suspend_point} points”)
elif suspend_point ==12:
print(f”License suspended.{suspend_point} points”)
break
for question number :: 3
Solution.
speed = int(input(‘Driver speed: ‘))
driver_speed = speed
distance = ()
demerit_point = (0)
if driver_speed 70:
demerit_point = (driver_speed – 70) / 5
print(f’Demerit_point: {demerit_point}’)
if demerit_point > 12:
print(‘Licence Expired’)
Hello Mr Mosh. First i would like to say thank you for your wonderful tutorials, it has really helped me as an up coming programmer.
I would like to ask please can you do a video tutorial for UI IX designing for beginners, containing necessary things we need. Thank you very much.
Thank you very much