#1 write a function to check whether a number falls in a given range
def check(n):
if n in range(1,100):
print(" %s is in the range"%str(n))
else :
print(" %s is in outside the given range"%str(n))
check(99)
check(100)
99 is in the range 100 is in outside the given range
#2 Some board games require you to reduce the number of cards you are holding by half, rounded down
For instance, if you have 10 cards, you would reduce to 5 and if you had 11 cards you would also reduce to 5.
Write a program that asks the user to enter how many cards they have and print out what their hand would reduce to
under this rule.
cards = eval(input('How many cards do you have: '))
print('After reducing you have:', cards // 2)
How many cards do you have: 4 After reducing you have: 2
#3 Write a program that asks the user to enter a positive integer.
Then generate a random number between that number and 10 more than that number
and print the letter A that many times on the same line.
from random import randint
num = eval(input('Enter a positive integer: '))
num_times = randint(num,num+10)
for i in range(num_times):
print('A', end=' ')
print()
Enter a positive integer: 10 A A A A A A A A A A A
#4 This is a very simple billing program.
Ask the user for a starting hour and ending hour, both given in 24-hour format (e.g., 1 pm is 13, 2 pm is 14, etc.).
The charge to use the service is $5.50 per hour. Print out the user’s total bill.
You can assume that the service will be used for at least 1 hour and never more than 23 hours.be careful to take care of the
case that the starting hour is before midnight and the ending is after midnight
start = eval(input('Enter starting hour (0-23): '))
end = eval(input('Enter ending hour (0-23): '))
if(end>=start):
print('Total: ',(end-start)*5.50)
else:
print('Total: ',(24-start + end)*5.50)
Enter starting hour (0-23): 10 Enter ending hour (0-23): 5 Total: 104.5
#5 One way to estimate probabilities is to run what is called a computer simulation.
Here we will estimate the probability of rolling doubles with two dice (where both dice come out to the same value).
To do this, run a loop 10,000 times in which random numbers are generated representing the dice and a count is kept of how
many times doubles appear.Print out the final percentage of rolls that are doubles.
from random import randint
count = 0
for i in range(10000):
r1 = randint(1, 6)
r2 = randint(1, 6)
if r1 == r2:
count += 1
print('Percentage of doubles:', 100*count/10000)
Percentage of doubles: 17.0