#1.Write a function to check whether a number falls in a given range
n=int(input("Enter a number"))
start_num=int(input("Enter range start number"))
#range(n)
range(start_num)
if n<=start_num-1:
print(n,"is available in the range")
else:
print(n,"is not available in the range")
#range(start,stop)
stop_num=int(input("Enter range stop number"))
range(start_num,stop_num)
if(n>=start_num & n<=stop_num):
print(n,"is available in the range")
else:
print(n,"is not available in the range")
Enter a number6 Enter range start number4 6 is not available in the range Enter range stop number7 6 is available in the 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. With 12 cards you would reduce to 6. 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.
n=int(input("Enter number of cards:"))
if n == 0 :
print("no cards in user hand")
elif n == 1 :
print("User have only 1 card")
elif n >= 2 :
res_hand = n/2
c = n%2
if c == 1 :
print("number of cards in hand:",int(res_hand))
else:
print("number of cards in hand:",int(res_hand))
Enter number of cards:45 number of cards in hand: 22
#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=int(input("Enter a number:"))
res_num=randint(num,num+10)
if res_num > 0 :
print("A" * res_num,end = " ")
Enter a number:2 AAAAAAAA
#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 time is after midnight.
start_hour = float(input("Enter Start hour in 24hr format:"))
end_hour = float(input("Enter End hour in 24hr format:"))
charge = 5.50
if (start_hour > end_hour):
end_hour = end_hour + 24.00
total_time = end_hour - start_hour
print("total time spend:",total_time)
if 23.00 >= total_time >= 1:
print("total bill:", total_time * charge)
else:
print("user must spend minimum of 1Hr and not more that 23Hrs:")
Enter Start hour in 24hr format:23 Enter End hour in 24hr format:22 23.0 total bill: 126.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
#while num <= 1 :
for num in range(1,10000) :
dice_1 = randint(1,6)
dice_2 = randint(1,6)
if dice_1 == dice_2 :
count = count + 1
print("Final percentage of getting doubles:", (count / 10000)*100)
Final percentage of getting doubles: 16.85