100% found this document useful (2 votes)
2K views

Write A Random Number Generator That Generates Random Numbers Between 1 and 6 (Simulates A Dice)

The document provides instructions for several Python programming exercises involving files, functions, variables, and random number generation. Specifically, it includes tasks to: 1) Read a text file line by line and display each word separated by #. 2) Read a text file and display counts of vowels, consonants, uppercase and lowercase letters. 3) Remove lines containing a specific character from one file and write to another. 4) Generate random numbers between 1 and 6 to simulate rolling a dice.

Uploaded by

Kartik Rawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views

Write A Random Number Generator That Generates Random Numbers Between 1 and 6 (Simulates A Dice)

The document provides instructions for several Python programming exercises involving files, functions, variables, and random number generation. Specifically, it includes tasks to: 1) Read a text file line by line and display each word separated by #. 2) Read a text file and display counts of vowels, consonants, uppercase and lowercase letters. 3) Remove lines containing a specific character from one file and write to another. 4) Generate random numbers between 1 and 6 to simulate rolling a dice.

Uploaded by

Kartik Rawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Write a program for using all the functions of math module.

2. Write a program for using all the function of statistics module.


3. Write a program for using all the function of random module.
4. Write a program for finding the factorial of a number and use default value as 5.
5. Write a program for sum of two numbers using user defined function with returning values to
the calling function.
6. Write a program for sum of n natural numbers using user defined function with passing n as
argument.
7. Write a program which shows the concept of local and global variable.
8. Read a text file line by line and display each word separated by a #
9. Read a text file and display the number of vowels consonants uppercase lowercase
10. Remove all the lines that contain the character 'a' in a file and write it to another file
11. Create a binary file with name and roll number and search for a given roll number and display
the name
12. Create a binary file with roll number, name and marks. Input a roll number and update the
marks.
13. Write a random number generator that generates random numbers between 1 and 6
(simulates a dice).
14. Create a CSV file by entering user id and password, read and search the password for given user
id.

1. Read a text file line by line and display each word separated by a #

file=open("test.txt","r")

lines=file.readlines()

for line in lines:

words=line.split()

for word in words:

print(word+"#",end="")

print(“ ”)

file.close()

2. Read a text file and display the number of vowels consonants uppercase lowercase

def cnt():
f=open("test.txt","r")

cont=f.read()

print(cont)

v=0

cons=0

l_c_l=0

u_c_l=0

for ch in cont:

if (ch.islower()):

l_c_l+=1

elif(ch.isupper()):

u_c_l+=1

ch=ch.lower()

if( ch in ['a','e','i','o','u']):

v+=1

elif (ch in ['b','c','d','f','g',

'h','j','k','l','m',

'n','p','q','r','s',

't','v','w','x','y','z']):

cons+=1

f.close()

print("Vowels are : ",v)

print("consonants are : ",cons)

print("Lower case letters are : ",l_c_l)

print("Upper case letters are : ",u_c_l)


#main program

cnt()

Remove all the lines that contain the character 'a' in a file and write it to another file

fin=open("test.txt","r")

fout=open("tey.txt","w")

s=fin.readlines()

print(s)

for j in s:

if 'a' in j:

fout.write(j)

fout.close()

fin.close()

Create a binary file with name and roll number. search for a given roll number and display the name
import pickle
import sys
dict={}
def write_in_file():
file=open("D:\\stud2.dat","ab") #a-append,b-binary
no=int(input("ENTER NO OF STUDENTS: "))
for i in range(no):
print("Enter details of student ", i+1)
dict["roll"]=int(input("Enter roll number: "))
dict["name"]=input("enter the name: ")
pickle.dump(dict,file) #dump-to write in student file
file.close()

def display():
#read from file and display
file=open("D:\\stud2.dat","rb") #r-read,b-binary
try:
while True:
stud=pickle.load(file) #write to the file
print(stud)
except EOFError:
pass
file.close()

def search():
file=open("D:\\stud2.dat","rb") #r-read,b-binary
r=int(input("enter the rollno to search: "))
found=0
try:
while True:
data=pickle.load(file) #read from file
if data["roll"]==r:
print("The rollno =",r," record found")
print(data)
found=1
break
except EOFError:
pass
if found==0:
print("The rollno =",r," record is not found")
file.close()

#main program
#Prepared by Ramesha K S

while True:
print("MENU \n 1-Write in a file \n 2-display ")
print(" 3-search\n 4-exit \n")
ch=int(input("Enter your choice = "))
if ch==1:
write_in_file()
if ch==2:
display()
if ch==3:
search()
if ch==4:
print(" Thank you ")
sys.exit()

5. create a binary file with roll number, name and marks. input a roll number and update the marks.

def Writerecord(sroll,sname,sperc,sremark):
with open ('StudentRecord.dat','ab') as Myfile:
srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc,
"SREMARKS":sremark}
pickle.dump(srecord,Myfile)

def Readrecord():
with open ('StudentRecord.dat','rb') as Myfile:
print("\n-------DISPALY STUDENTS DETAILS--------")
print("\nRoll No.",' ','Name','\t',end='')
print('Percetage',' ','Remarks')
while True:
try:
rec=pickle.load(Myfile)
print(' ',rec['SROLL'],'\t ' ,rec['SNAME'],'\t ',end='')
print(rec['SPERC'],'\t ',rec['SREMARKS'])
except EOFError:
break
def Input():
n=int(input("How many records you want to create :"))
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
sperc=float(input("Enter Percentage: "))
sremark=input("Enter Remark: ")
Writerecord(sroll,sname,sperc,sremark)
def Modify(roll):
with open ('StudentRecord.dat','rb') as Myfile:
newRecord=[]
while True:
try:
rec=pickle.load(Myfile)
newRecord.append(rec)
except EOFError:
break
found=1
for i in range(len(newRecord)):
if newRecord[i]['SROLL']==roll:
name=input("Enter Name: ")
perc=float(input("Enter Percentage: "))
remark=input("Enter Remark: ")

newRecord[i]['SNAME']=name
newRecord[i]['SPERC']=perc
newRecord[i]['SREMARKS']=remark
found =1
else:
found=0

if found==0:

print("Record not found")


with open ('StudentRecord.dat','wb') as Myfile:
for j in newRecord:
pickle.dump(j,Myfile)

def main():

while True:
print('\nYour Choices are: ')
print('1.Insert Records')
print('2.Dispaly Records')
print('3.Update Records')
print('0.Exit (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
Input()
elif ch==2:
Readrecord()
elif ch==3:
r =int(input("Enter a Rollno to be update: "))
Modify(r)
else:
break
main()

Write a random number generator that generates random numbers between 1 and 6 (simulates
a dice).
import random
import random

def roll_dice():
print (random.randint(1, 6))

print("""Welcome to my python random dice program!


To start press enter! Whenever you are over, type quit.""")

flag = True
while flag:
user_prompt = input(">")
if user_prompt.lower() == "quit":
flag = False
else:
print("Rolling dice...\nYour number is:")
roll_dice()

You might also like