Question-1. Write a program that asks the user to enter a letter. Then it generates a random number between 1 and 10 and prints out the letter that many times.
import random
userinput = input('enter a letter: ')
random_number = random.randint(1,10)
print("The random number generated is: ",random_number)
for i in range(random_number):
print(userinput)
enter a letter: l The random number generated is: 1 l
Question-2. In the game Yahtzee, players roll five dice. A Yahtzee is when all five dice are the same. Write a program that simulates rolling five 10,000 times and counts how many Yahtzees occur. Print out what percentage of the rolls come out to be Yahtzees.
import random
num_rolls = 10000
num_yahtzees = 0
for i in range(num_rolls):
dice = [random.randint(1, 6) for i in range(5)]
if len(set(dice)) == 1:
num_yahtzees += 1
percentage = (num_yahtzees / num_rolls) * 100
print("Percentage of Yahtzees:", percentage)
Percentage of Yahtzees: 0.13
Question-3. Write a program that asks the user to enter a sentence, removes all the spaces from the sentence, converts the remainder to uppercase, and prints out the result.
userinput_sentence = input('Enter a sentence: ')
sentence_nospaces = userinput_sentence.replace(" ","")
upper_sentence = sentence_nospaces.upper()
print(upper_sentence)
Enter a sentence: hi how are you? HIHOWAREYOU?
Question-4. Write a program that asks the user to enter a string. If the string is at least five characters long, then create a new string that consists of the first five characters of the string along with three asterisks at the end. Otherwise add enough exclamation points (!) to the end of the string in order to get the length up to five.
user_input = input("Enter a string: ")
if len(user_input) >= 5:
new_string = user_input[:5] + '***'
else:
new_string = user_input + '!' * (5 - len(user_input))
print("Modified string:", new_string)
Enter a string: vikas Modified string: vikas***
Question-5. Write a program that ask the user to enter a string that consists of multiple words. Then print out the first letter of each word, all on the same line.
user_input = input("Enter a string with multiple words: ")
words = user_input.split()
first_letters = [word[0] for word in words]
result = ' '.join(first_letters)
print("First letters of each word:", result)
Enter a string with multiple words: vikas is a good boy First letters of each word: v i a g b