In: Computer Science
UsePython (import numpy as np) use containers (Branching if statement, while loop, for loop, numpy Array)
Implement an algorithm to guess a random number that the computer generates. The random number must be an integer between 1 and 1000 (inclusive). For each unsuccessful attempt, the program must let the user know whether to deal with a higher number or more. low. There is no limit on the number of attempts, the game only ends when the user succeeds. The user starts the game with $ 1000 and for each unsuccessful attempt loses $ 100. If you repeat any number of those who had already entered, they lose $ 200 instead of $ 100. If you don't follow the immediate instruction, you lose $ 200 instead of $ 100 (for example, if the program says to try a higher number, but you enter a higher number low to the previous one). In each unsuccessful attempt the program must tell the user how much money he has left (the figure can be negative). If the user hits the number and still has money, the program should congratulate him and tell him how much he won. If the user hits the number and owes money, the program must “bully” him and charge him. If the user hits the number by closing at $ 0 (neither wins nor owes), the program must let them know. In your report present 4 "print screens" of the execution of your program that result in 4 different results and their respective desktop tests (note that this is a special case where you have to run the program before the desktop test as there is no way I can control the random number that will come out in each run).
#import module
import numpy as np
#numpy array to store entered values
arr=np.arange(0)
#generate a random number in [1,1000]
num=np.random.randint(1,1000)
#initial amount
amount=1000
#for the first time
print("Guess the number: ")
#bool to store if previous instruction was to guess higher or
lower
flag=True
#if first turn then start is True otherwise false
start=True
#to save previous guessed number
prev=0
#loop until user guess the number
while True:
#take user input
guess=int(input())
#if guess is correct then break
if guess==num:
break
#otherwise
#if guess is greater
if guess>num:
print("oops! guess a lower number than this")
#if guess is lower
elif guess<num:
print("oops! guess a higher number than this")
#if first turn
if start:
#add this guess to array
arr=np.append(arr,guess)
#update previous
prev=guess
#deduct amount
amount-=100
#print left amount
print("You only have {}".format(amount))
#make start false
start=False
#if guess is greater
if guess>num:
flag=False
#if guess is lower
elif guess<num:
flag=True
continue
#if current guess is already guessed previously then
if guess in arr:
amount-=200
#otherwise
else:
#if previous instruction was to guess a higher number
if flag:
#if current guess is lower than the previous then
if guess<prev:
amount-=200
#otherwise
else:
amount-=100
#if previous instruction was to guess a lower number
elif not flag:
#if current guess is higher than the previous
if guess>prev:
amount-=200
#othewise
else:
amount-=100
#update flag
if guess>num:
flag=False
elif guess<num:
flag=True
#print left amount
print("You only have {}".format(amount))
#update previous
prev=guess
#add this guess to array
arr=np.append(arr,guess)
#print newline
print()
#after guessing the correct number
#if amount is greater than 0
if amount>0:
print("Congratulations! you won {}".format(amount))
#if amount is less than 0
elif amount<0:
print("Hey loser you owe me {}".format(-amount))
#if amount is 0
else:
print("You neither win nor owe me any money!")
Guess the number: 10 oops! guess a higher number than this You only have 900 10 oops! guess a higher number than this You only have 700 100 oops! guess a higher number than this You only have 600 500 oops! guess a lower number than this You only have 500 300 oops! guess a higher number than this You only have 400 400 oops! guess a higher number than this You only have 300 450 oops! guess a higher number than this You only have 200 475 oops! guess a lower number than this You only have 100 460 oops! guess a higher number than this You only have 0 470 oops! guess a higher number than this You only have -100 473 Hey loser you owe me 100
Guess the number: 300 oops! guess a higher number than this You only have 900 200 oops! guess a higher number than this You only have 700 250 oops! guess a higher number than this You only have 600 450 oops! guess a lower number than this You only have 500 430 oops! guess a lower number than this You only have 400 425 oops! guess a higher number than this You only have 300 428 Congratulations! you won 300
Guess the number: 10 oops! guess a higher number than this You only have 900 100 oops! guess a higher number than this You only have 800 500 oops! guess a higher number than this You only have 700 800 oops! guess a higher number than this You only have 600 900 oops! guess a higher number than this You only have 500 950 oops! guess a higher number than this You only have 400 975 oops! guess a lower number than this You only have 300 960 oops! guess a higher number than this You only have 200 965 oops! guess a higher number than this You only have 100 968 oops! guess a higher number than this You only have 0 969 You neither win nor owe me any money!
Guess the number: 500 oops! guess a higher number than this You only have 900 600 oops! guess a lower number than this You only have 800 500 oops! guess a higher number than this You only have 600 590 oops! guess a lower number than this You only have 500 560 oops! guess a higher number than this You only have 400 570 oops! guess a higher number than this You only have 300 578 Congratulations! you won 300
CODE
INPUT/OUTPUT
OUTPUT1
OUTPUT2
OUTPUT3
OUTPUT4
So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.