Question
# This program will contain random number file writer to be read in the next program # Import random number so that random numbers may
# This program will contain random number file writer to be read in the next program
# Import random number so that random numbers may be generated
import random
filename = 'randomnum.txt' # Creating a file
file = open(filename, 'w')
while(True):
try:
n= int(input('How many random numbers do you want? '))
if n<=0:
print('Invalid input. no negative or 0 numbers just postive integers try again. How many random numbers do you want? ')
continue
except ValueError:
print(' The value you entered is invalid. Only numerical values are valid try again. ')
else:
break
while(True):
try:
low=int(input('What is the lowest the random number should be? '))
if low<=0:
print(' Invalid input. no negative or 0 numbers just positive integers try again. What is the lowest the random number should be? ')
continue
except ValueError:
print(' The value you entered is invalid. Only numerical values are valid try again. ')
else:
break
while(True):
try:
high=int(input('What is the highest the random number should be: '))
if high<=0:
print(' Invalid input. no negative or 0 numbers just positive integers try again. What is the highest the random number should be? ')
continue
except ValueError:
print(' The value you entered is invalid. Only numerical values are valid try again. ')
else:
break
print('The random numbers were written to ', filename)
for i in range(n):
n=random.randint(low, high)
file.write(str(n)+' ')
file.close()
How do I get the program to display the random numbers were written to the file. like the last code. When I run the program it just stops at the highest the random number should be. and make sure that it is written to the file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started