Question

In: Computer Science

The question: Write a program in Python that writes four random integers in range 1-100 on...

The question: Write a program in Python that writes four random integers in range 1-100 on a file named 'num.txt'. Write try-except block to handle at least two standard python error (any two errors).

Hints:

import random

def main():
    # Local variables

    # Open output file.

    # Write random numbers to the file.

    # Write it on to the file. 

    # Close the file.
    

# Call the main function.
main()

Here is my answer: please let me know if anything is wrong with it. thanks

"

import random

# define the min and max of the random numbers
min = 1
max = 100

def main():


#get four random numbers from 1 to 100 or min to max
number1 = random.randint(min,max)
number2 = random.randint(min,max)
number3 = random.randint(min,max)
number4 = random.randint(min,max)
  
#write TRY/EXCEPT statment
try:

#OPEN a file named num.txt
outfile = open('num.txt', 'w')
  
#WRITE the random numbers to the file
outfile.write(str(number1) + '\n')
outfile.write(str(number2) + '\n')
outfile.write(str(number3) + '\n')
outfile.write(str(number4) + '\n')
  
#PRINT the numbers
print(number1)
print(number2)
print(number3)
print(number4)

  

#write TRY/EXCEPT statment
except ValueError as err:
print(err)

except IOError:
print("An error occured trying to read the file.")


except KeyboardInterrupt:
print("Something went wrong when writing to the file.")
  
except:
print("An error occured.")

finally:
outfile.close()


#call the function
main()


"

Solutions

Expert Solution

Here is the answer..

CODE:

import random
# define the min and max of the random numbers
min = 1
max = 100
def main():
#get four random numbers from 1 to 100 or min to max
number1 = random.randint(min,max)
number2 = random.randint(min,max)
number3 = random.randint(min,max)
number4 = random.randint(min,max)
#write TRY/EXCEPT statment
try:
#OPEN a file named num.txt
outfile = open('num.txt', 'w')
  
#WRITE the random numbers to the file
outfile.write(str(number1) + '\n')
outfile.write(str(number2) + '\n')
outfile.write(str(number3) + '\n')
outfile.write(str(number4) + '\n')   
#PRINT the numbers
print(number1)
print(number2)
print(number3)
print(number4)
  
#write TRY/EXCEPT statment
except ValueError as err:
print(err)
except IOError:
print("An error occured trying to read the file.")

except KeyboardInterrupt:
print("Something went wrong when writing to the file.")
except:
print("An error occured.")

finally:
outfile.close()
#call the function
main()

output:

When we press ctrl+c

if you have any doubts please COMMENT...

I think there are no errors in your code.if you didn't get output please check indentation

If you understand the answer please give THUMBS UP...


Related Solutions

Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are...
One Problem/Question (Four Parts) Write a python program which prints a sequence of integers that are multiples of 10, starting at 0 and ending at 100. Write a python program that computes the average of 10 random odd integers, ranging from 0 to 500. Write a python program that prints whatever the user enters, and stops when the user types “done”, using an infinite loop and a break statement. Same as number three but use a condition. The loop terminates...
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
1. Write a python program to create a list of integers using random function. Use map...
1. Write a python program to create a list of integers using random function. Use map function to process the list on the expression: 3x2+4x+5 and store the mapped elements in another list. Now use filter to do sum of all the elements in another list. 2. Write a function that takes n as input and creates a list of n lists such that ith list contains first 10 multiples of i. 3. Write a function that takes a number...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT