Question

In: Computer Science

UsePython (import numpy as np) use containers (Branching if statement, while loop, for loop, numpy Array)...

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).

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in PYTHON with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • In the below code the random number game is implemented using numpy array , if consition, while loop etc and for all the cases where final amount is less than zero , zero, more than zero are shown in the output. And whenever user guessed a previously guessed number then 200 is deducted and whenever user unfoolow the immediate instruction then also 200 is deducted otherwise 100 is deducted. For details see the code below.
  • CODE:

#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!")

  • INPUT/OUTPUT:
  1. 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
  2. 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
  3. 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!
  4. 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
  • Below are the screenshot attached for the code and input/output for better clarity and understanding.

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.


Related Solutions

In python import numpy as np Given the array b = np.arange(-6, 4) compute and print...
In python import numpy as np Given the array b = np.arange(-6, 4) compute and print 1.) The array formed by \(bi^2 - 1\), where the \(bi\) are the elements of the array b. 2.) The array formed by multiplying b with the scalar 100. 3.)The array formed by 2.0 b i in reverse order. (Note: the base 2.0 must be a floating point number; for integer values a ValueError: Integers to negative integer powers are not allowed. is raised.)...
import math import numpy as np import numpy.linalg from scipy.linalg import solve A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0,...
import math import numpy as np import numpy.linalg from scipy.linalg import solve A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0, 0, 0], [-math.sin(math.pi/6), 0, -math.sin(math.pi/3), 0, 0, 0], [math.cos(math.pi/6), 1, 0, 1, 0, 0], [math.sin(math.pi/6), 0, 0, 0, 1, 0], [0, -1, -math.cos(math.pi/3), 0, 0, 0], [0, 0, math.sin(math.pi/3), 0, 0, 1]]) b = np.array([0, 2000, 0, 0, 0, 0]) x = [0, 0, 0, 0, 0, 0] def seidel(a, x, b): # Finding length of a(3) n = len(a) # for loop for...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...
Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path): """ Retrieve list of students and their expected grade from file, generate a sampled test grade for each student drawn from a Gaussian distribution defined by the student expected grade as mean, and the given standard deviation. If the sample is higher than 100, re-sample. If the sample is lower than 0 or 5 standard deviations below mean, re-sample Write the list of student...
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as...
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as np from future import print_function # Definisi class ArrayStack class ArrayStack(object): def __init__(self): self.data = np.zeros(20, dtype=np.int) self.count = 0 def isEmpty(self): pass # ubah saya def size(self): pass # ubah saya def push(self, obj): pass # ubah saya def pop(self): pass # ubah saya #-------------------------- # End of class if __name__ == "__main__": stack = ArrayStack() randn = np.random.permutation(1000) for i in range(1000):...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Use NumPy to create a My_Array; a 3 by 3 array where in,m = n +...
Use NumPy to create a My_Array; a 3 by 3 array where in,m = n + m. (e.g., the first row first column would be 0+0=0, second row first column would be 1+0=1, etc). complete the following using NumPy: Compute the mean, median, range, and variance of this array Find the inverse or pseudo inverse Find the determinant Perform the following operations on My_Array Create My_1D_Array by reshaping My_Array into a 1-dimensional array Create a new array that is the...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT