Skip to content

Instantly share code, notes, and snippets.

@Venkat-Swaraj
Created May 7, 2022 09:55
Show Gist options
  • Save Venkat-Swaraj/e9b5353adec3436a386ca17628a9a5f2 to your computer and use it in GitHub Desktop.
Save Venkat-Swaraj/e9b5353adec3436a386ca17628a9a5f2 to your computer and use it in GitHub Desktop.
sum guesser problem
program in the file Observe.py that prints 10 random integers (each random integer should have a value between 0 and 100, inclusive).
program uses a constant named NUM_RANDOM, which determines the number of random numbers to print (with a value of 10).
It also uses constants named MIN_RANDOM and MAX_RANDOM to determine the minimal and maximal values of the random numbers generated (with respective values 0 and 100).
To generate random numbers, you should use the function random.randint() from Python’s random library.
Here's a sample run of the program:
$ python random_numbers.py
35
10
45
59
45
100
8
31
48
6
Run it now and learn by observation
Now your TASK:
After you have observed the above program please do the following
in Problem2.py
Now that you’ve seen how programming can help us in a number of different areas, it’s time for you to implement a program that helps other people learn! In this problem, you’ll write a program in the file Problem2.py that randomly generates a simple addition problem for the user, reads in the answer from the user, and then checks to see if they got it right or wrong. Note that “console” is another name for “terminal” :-).
More specifically, your program should be able to generate simple addition problems that involve adding two 2-digit integers (i.e., the numbers 10 through 99). The user should be asked for an answer to the generated problem. Your program should determine if the answer was correct or not, and give the user an appropriate message to let them know.
A sample run of the program is shown below (user input is in bold italics).
$ python3 Problem2.py
What is 51 + 79?
Your answer: 120
Incorrect. The expected answer is 130
Here's another sample run, where the user gets the question correct:
$ python3 Problem2.py
What is 55 + 11?
Your answer: 66
Correct!
# This is code solution
"""
Prints out a randomly generated addition problem
and checks if the user answers correctly.
"""
import random
def main():
num1 = random.randint(10,100)
num2 = random.randint(100,200)
ans = num1 + num2
print(f"What is {num1} + {num2}: ")
value = int(input())
if ans != value:
print("You are worng go to nursery")
else:
print("You are right")
if __name__ == '__main__':
main()
"""
Prints out 10 random numbers between 0 and 100.
"""
import random
NUM_RANDOM = 10
MIN_RANDOM = 0
MAX_RANDOM = 100
def main():
for i in range(NUM_RANDOM):
rand_num = random.randint(MIN_RANDOM, MAX_RANDOM)
print(rand_num)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment