Sunday, February 23, 2020

Python Project Problem 1

Question:
Make a program which randomly chooses a number to guess and then the user will have a few chances to guess the number correctly. In each wrong attempt, the computer will give a hint that the number is greater or smaller than the one you have guessed.

Answer:
import secrets

n1 = secrets.randbits(7)
n2 = secrets.randbits(7)

if (n1 > n2):
    beg = n2
    end = n1
else:
    beg = n1
    end = n2

import random

rand = random.randrange(beg, end, 1)

print("NUMBER GUESSING GAME.....")

print("Guess a number(between ", beg, " and ", end, "): ", end='');
num = int(input())

while (num != rand):
    if (num > rand):
        print("Your guess was too high: Guess a number lower than ", num);
        num = int(input())
    else:
        print("Your guess was too low: Guess a number higher than ", num);
        num = int(input())

print("Congratulation YOU WON!!!")
OUTPUT:

NUMBER GUESSING GAME.....
Guess a number(between  58  and  87 ): 62
Your guess was too low: Guess a number higher than  62
86
Your guess was too high: Guess a number lower than  86
87
Your guess was too high: Guess a number lower than  87
65
Your guess was too low: Guess a number higher than  65
69
Your guess was too low: Guess a number higher than  69
72
Your guess was too low: Guess a number higher than  72
75
Your guess was too low: Guess a number higher than  75
79
Your guess was too high: Guess a number lower than  79
77
Congratulation YOU WON!!!

No comments:

Post a Comment