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....
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.
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.
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 python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so”, “Yes, of course!”, “I’m not sure”, and so forth. The program should read the responses from the file into a list. It should prompt the user to ask...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays...
Write a program that simulates a Magic 8 Ball, which is a fortune-telling toy that displays a random response to a yes or no question. In the student sample programs for this book, you will find a text file named 8_ball_responses.txt. The file contains 12 responses, such as “I don’t think so,” “Yes, of course!,” “I’m not sure,” and so forth. The program should read the responses from the file into an array or ArrayList object. It should prompt the...
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT