Best practices (5) Flashcards

1
Q

Write a program that asks the user for 10 numbers and counts how many of those numbers are greater than 10.

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a program that counts how many of the squares from 1 to 100 end in a 4.

A

count=0
for i in range(1, 101):
if(i**2)%10==4:
count=count+1
print(count)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a program that calculates the sum of all numbers from 1 to 100.

A

s=0
for i in range(1,101):
s=s+i
print(“The sum is:”, sum)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a program that askes the user for 10 numbers and calculates the average of all the numbers.

A

s=0
for i in range(10):
num=eval(input(“Give me a number: “))
s=s+num
print(“The average is: “, s/10)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a program that asks the user for a number and checks whether the number is a prime or not.

A

num=eval(input(“Give me a number: “))
if num%i==0:
print(“Not a prime”)
else:
print(“Prime”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a program that asks the user to provide 5 numbers and then finds the largest number.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a program that writes “Hello” a random number of times, anywhere between 5 and 25. (2 variante)

A

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”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a program that writes “Hello” (vertically and horizontally) a random number of times. (2 variante)

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a program that generates 10000 numbers and checks how many random numbers from 1 to 100 are multiples of 12:

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a program that counts how many squares of the numbers from 1 to 100 end in a 1.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly