Question

In: Computer Science

I need to implement a code in python to guess a random number. The number must...

I need to implement a code in python to guess a random number. The number
must be an integer between 1 and 50 inclusive, using the module random function randint. The user will have four options to guess the number. For each one of the failed attempts, the program
it should let the user know whether is with a lower or higher number and how many tries they have left. If the user guess the number, the program must congratulate the user and tell him how much he won. If you are a user, hit on the 1st attempt
He wins first $ 100, on the second $ 75, on the third $ 50, on the fourth $ 25. If the user misses all four opportunities, the program must inform the user that he owes money  $ 60 and what was the number that the computer generates randomly.

Solutions

Expert Solution

I will give the code , also the screen shot of code and output:

code:

import random
hiddenNumber = random.randint(1,50)
won = False # to check if the user won or not
number_of_gusses = 0
for i in range(4): # number of gusses is 4
  n = int(input("Gussess the number : "))
  if n == hiddenNumber:
    won = True
    if i == 0:
      amt = 100
    elif i == 1:
      amt = 75
    elif i == 2:
      amt = 50
    else:
      amt = 25
    print("Congratulations! You gusses the number in ",i+1," attempts and won $",amt)
    break
  else:
    if hiddenNumber > n: 
      print("The  entered number is smaller than hidden number. Try again,")
    else:
      print("The  entered number is greater than hidden number. Try again,")
    print("Tries left :",4-i-1)
if not won:
  print("Sorry!! You did not managed to guess the number . You had reached the gussessing limit.")
  print("You  owes money  $ 60 ,The correct number is  : ",hiddenNumber)

Screenshot of outputs:

I had managed to find a number by gussing:

Ouput case , where i was not able to get the correct result in 4 attempts:

Screenshot of code:

----------------------------------------------------------------------------------------------------------------

Note :Refer to the screenshot of the code to understand the indentation of the code.

Comment below, if you need to clear any more doubts regarding this question. Please upvote if you are happy. Thank you sir.


Related Solutions

lets say i make a PYTHON code that lets a user guess a number between 1-1000,...
lets say i make a PYTHON code that lets a user guess a number between 1-1000, every failed attempt you get a hint (go lower or go higher) how can i penalize the user if they dont follow the hints, example ( go higher!... your next pick: a smaller number... 2 you loose $100 for not following hint) 2 and the user has unlimited attempts, until he guesses the number, this is made using random.randint(1,1000) function THE ONLY THING IM...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
(Python Code please) Guess the number! You will add to the program you created last week....
(Python Code please) Guess the number! You will add to the program you created last week. This week you will add quite a bit of code to your project. You will add an option for the computer to guess as well as the user to guess a number. In addition, you will add a menu system. Be sure to import random at the beginning of your code and use a comment block explaining what your program does #Guess the number...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
I need to add this checkpoint to an existing code that i have in python Checkpoint...
I need to add this checkpoint to an existing code that i have in python Checkpoint 1: Once you have created and tested the Bank Account Class create subclasses to the BankAccount class. There should be two subclasses, CheckingAccount and SavingsAccount. You should add interest_rate to the parent BankAccount class. To the CheckingAccount add variables called per_check_fee default to false and allow_overdraft default to True. To SavingsAccount add the variable transactions_per_month, default it to 5. Create instances of CheckingAccount and...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random matrix size 5x5.
It is a strict requirement that this code is implemented in python. The code must be...
It is a strict requirement that this code is implemented in python. The code must be implemented from scratch not using e.g. numpy etc. a. Create a function that takes a matrix X of all sizea as input and gives the transposed matrix as output. Example transposeMatrix([[1,2] ,[3,4] ,[5,6]]) = [[1,3,5] ,[2,4,6]] b. Create a function that multilpies a matrix X1 and X2 of all sizes. Example x1 = [[0,1] ,[1,0] ] x2 = [[1, 0] ,[0,-1] ] matrtixMultl(x1,x2) =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT