Question

In: Computer Science

Q3. Write a program that simulates the 4-digit lock of the suitcase. You need to represent...

Q3. Write a program that simulates the 4-digit lock of the suitcase. You need to represent each rotatable cyllinder with some variable, it's value is going to reflect what state your lock is in. Digits are going to be 0 through 9. If 9 rotates further, it gets back to 0. If 0 rotates back, it becomes 9, just like in real suitcase locks.

Then, pick a combination that opens your suitcase, like:
1-9-9-5
And pick a random starting combination.

Write functions to allow you to change 1 digit at a time. After each change, you should check whether all 4 digits match the opening combo. If they do, show the last state and stop program, if they do not, just display the state of your lock and allow user to keep rotating.

Solutions

Expert Solution

Code in Python:-

import random
def lock():
    a,b,c,d = 1,9,9,5
    i = random.randint(0,9)
    j = random.randint(0,9)
    k = random.randint(0,9)
    l = random.randint(0,9)
    print('RULES:-----')
    print('     1). Enter the value of place  (between -1 to -4 or  1 to 4 inclusive) to change the integer at that place by 1 ')
    print('     2). positive integer is for forward rotation i.e 9 to 0')
    print('     3). negative integer is for backward rotation i.e 0 to 9')
    
    print('\n')
    
    if input('Want to play this game?(y/n)') == 'y':
        print("Initial Lock Configuration---> ",i,j,k,l)
        while True:
            input_choice = int(input('Enter the place of key you want to rotate:- '))
            if abs(input_choice) == 1:
                if abs(input_choice) == input_choice:
                    i = (i+1)%10
                else:
                    i = (i-1+10)%10                  # +10 to check for negative cases
            elif abs(input_choice) == 2:
                if abs(input_choice) == input_choice:
                    j = (j+1)%10
                else:
                    j = (j-1+10)%10
            elif abs(input_choice) == 3:
                if abs(input_choice) == input_choice:
                    k = (k+1)%10
                else:
                    k = (k-1+10)%10
            elif abs(input_choice) == 4:
                if abs(input_choice) == input_choice:
                    l = (l+1)%10
                else:
                    l = (l-1+10)%10
            else:
                print('Invalid key ! Enter number between -1 to -4 or 1 to 4')
            
            if (a == i and b == j and c == k and d == l):
                print('-----------------LOCK Open ------------> KEY = ',a,b,c,d)
                break
            else:
                print('LOCKED! Try again--->',i,j,k,l)
    else:
        print('Will play next time')

lock()


          
Output:-
  
      

Snapshot of the code:-


Related Solutions

Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value. Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer....
Write a C++ program that checks if the password is correct. The password is a 4-digit...
Write a C++ program that checks if the password is correct. The password is a 4-digit number combination. The program repeats to ask the password until the password is correct or you enter -1 to exit. Input: The password is set to ‘1123’. A user input the password to proceed, or -1 to exit. Sample Output: The program should display the following output. (The red text is a user input.) Test case 1: when you enter the correct password. Enter...
Design and Write a program that asks for a 4-digit year and determines whether that year...
Design and Write a program that asks for a 4-digit year and determines whether that year is a leap year or not. This should work for any year from 1700 to 2022. Anything not completely obvious to a newbie must use # notes on lines to explain. Must be modularized and repeatable. Should have an invocation of the main routine at the end of the program. Must have Flowgorithm and Python code.
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this program, your code can have user defined functions (but not required), the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Requirements: There is a customer with the following credential and can only access the system if the Access Code is correct: • Name: Peter Parker, Access Code: 2222 • When the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Write a regular expression that can represent any digit, without more than one digit or non...
Write a regular expression that can represent any digit, without more than one digit or non digit characters.
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
Write a java program that simulates thousands of games and then calculate the probabilities from the...
Write a java program that simulates thousands of games and then calculate the probabilities from the simulation results. Specifically in the game, throw two dice, the possible summations of the results are: 2, 3, ..., 12. You need to use arrays to count the occurrence and store the probabilities of all possible summations. Try to simulate rolling two dice 100, 1000, 10,0000 times, or more if needed. Choose one simulation number so that the probabilities you calculated is within 1%...
3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT