top of page
  • Pinterest
  • Facebook
  • Instagram

NumGenerator

  • Writer: Lim Long Teck
    Lim Long Teck
  • Jun 24, 2021
  • 1 min read

Write a program that generates two integers between 0 and 100 inclusive and prompts the user to enter the sum of these 2 integers. The program reports if the answer is correct or wrong (program will also print the correct answer if the user answer is wrong).


Program:

import random

num1 = random.randint(0,100)

num2 = random.randint(0,100)

sum = num1+num2

ans = int(input('Enter the sum of {} and {}: '.format(num1,num2)))

if sum == ans:

print('Your answer is correct!')

else:

print('Your answer is wrong.')

print('The correct asnwer is {}.'.format(sum))


Output:

Enter the sum of 72 and 12: 84

Your answer is correct!

---------------------or---------------------

Enter the sum of 72 and 12: 89

Your answer is wrong.

The correct answer is 84.





Recent Posts

See All
CountLetters

Write a Python program to - Prompt user to input a sentence - Count the occurrence of each letter in the input sentence - Save the result...

 
 
 
BirthdayParadox

The Birthday Paradox asks the question, “If you asked random people their birthday, how many people do you expect to ask before you find...

 
 
 
ChristmasTree

Write a program to prompt the user for a character and a number, and then print a Christmas tree accordingly. Program: char =...

 
 
 

Comments


bottom of page