Best practices (5) Flashcards
Write a program that asks the user for 10 numbers and counts how many of those numbers are greater than 10.
count=0
for i in range(10):
num=int(input(“Enter a number: “)
if num>10:
count=count+1
print(“There are”, count, “numbers greater than 10”)
Write a program that asks the user for 10 numbers and counts how many numbers are greater than 10 and how many numbers are equal to 10.
count1=0
count2=0
for i in range(10):
num=int(input(“Enter a number:”))
if num>10:
count1=count1+1
if num==10:
count2=count2+1
print(“There are”, count1, “numbers greater than 10”)
print(“There are”, count2, “numbers equal to 10”)
Write a program that counts how many of the squares from 1 to 100 end in a 4.
count=0
for i in range(1, 101):
if(i**2)%10==4:
count=count+1
print(count)
Write a program that calculates the sum of all numbers from 1 to 100.
s=0
for i in range(1,101):
s=s+i
print(“The sum is:”, sum)
Write a program that askes the user for 10 numbers and calculates the average of all the numbers.
s=0
for i in range(10):
num=eval(input(“Give me a number: “))
s=s+num
print(“The average is: “, s/10)
Write a program that asks the user for a number and checks whether the number is a prime or not.
num=eval(input(“Give me a number: “))
if num%i==0:
print(“Not a prime”)
else:
print(“Prime”)
Write a program that asks the user to provide 5 numbers and then finds the largest number.
largest=int(input(“Give me a positive number: “)
for i in range(5):
num=int(input(“Give me a positive number: “)
if num>largest:
largest=num
print(“Largest number: “, largest)
Write a program that writes “Hello” a random number of times, anywhere between 5 and 25. (2 variante)
import random from randint
num=randint(5,25)
for i in range(num):
print(“Hello”)
import random from randint
for i in range(randint(2,25):
print(“Hello”)
Write a program that writes “Hello” (vertically and horizontally) a random number of times. (2 variante)
import random from randint
num=randint(1,9)
for i in range(num):
print(“ Hello”*num)
inport random fron randint
for i in range(7):
num=randint(1, 6)
print(“Hello”*num)
Write a program that generates 10000 numbers and checks how many random numbers from 1 to 100 are multiples of 12:
from random import randint
count=0
for i in range(10000):
num=randint(1,100)
if num%12==0:
count=count+1
print(“Number of multiples by 12: “, count)
Write a program that counts how many squares of the numbers from 1 to 100 end in a 1.
count=0
for i in range(1, 101):
num=i⁎i
if num%10==1:
count=count+1
print(“Number of squares that end in 1: “, count)
Write a program that counts how many of the aquares of the numbers from 1 to 100 end in a 4 and how many end in a 9.
count=0
for i in range(1, 101):
num=i⁎i
if num%10==9 or num%10==4:
count=count+1
print(“Number of squares that end in 9 and 4: “, count)