Posts

Showing posts with the label Exercises

Python Exercise 7 --- "Snake, Water, Gun"

 Python Exercise 7 --- "Snake, Water, Gun" Today's task for you guys will be to develop your first-ever Python game i.e.,  "Snake Water Gun." (Just like --- Rock, Paper, Scissor) Most of you must already be familiar with the game. Still, I will provide you with a brief description. This is a two-player game where each player chooses one object.  As we know, there are three objects, snake, water, and gun. So, the result will be  Snake vs. Water: Snake drinks the water hence wins. Water vs. Gun: The gun will drown in water, hence a point for water Gun vs. Snake: Gun will kill the snake and win. In situations where both players choose the same object, the result will be a draw. Now moving on to instructions: You have to use a random choice function that we studied in tutorial #38, to select between, snake, water, and gun. You do not have to use a print statement in case of the above function. Then you have to give input from your side. After getting ten consecutive...

Python Tutorial 24 --- "Using Python External & Built In Modules"

Python Tutorial 24 ---   "Using Python External & Built In Modules" DOC: In Python, a module can be defined as a file containing a runnable python code.  The extension used by modules in Python is .py.  Hence any simple file containing Python code can be considered a module if its extension is .py. The file and module names are the same; hence we can call a module only by name without using the extension. Along with this, a module can define functions, classes, and variables. There are two types of modules. Built-in External Built-in Modules:             Built-in modules are already installed in Python by default. We can import a module in Python by using the import statement along with the specific module name. We can also access the built-in module files and can read the Python code present in them. Newly integrated modules are added in Python witch each update.  Some important modules names are as follows ...

Python Exercise 5 --- "Astrologer's Stars"

 Python  Exercise 5 --- "Astrologer's Stars" In today’s exercise, what we have to do is, we have to print a pattern similar to that of  a  right-angle triangle , such as: * ** ** * ** ** Copy You have to follow certain instructions, which are as follows: You have to take an integer type variable, and the input of the variable will define the length of the triangle. You have to declare another Boolean variable. When the value of Boolean is 1 i.e. True, the pattern will be printed as shown above. But if the value of Boolean is 0 or false, then the triangle will be printed upside down . Solution: a = int ( input ( "Enter the number of lines you want to print \n " )) b = bool ( int ( input ( "Enter 0 or 1 \n " ))) def star (a , b): if b == True : c = 1 while c <= a: print (c * "*" ) c = c + 1 else : while a > 0 : print (a * "*" ) a = a - 1 star(a , ...

Python Practice Program 1

 Python Practice Program 1 Write a Python program which will keep adding a stream of numbers inputted by the user. The adding stops  as soon as the user presses q key on keyboard. Solution: """ Write a Python program which will keep adding a stream of numbers inputted by the user. The adding stops as soon as the user presses q key on keyboard. """ sum = 0 while True : userInput = input ( "Enter the item price or press q to quit: \n " ) if (userInput!= 'q' ): sum = sum + int (userInput) print ( f"Ordered total so far { sum } " ) else : print ( f"Your total bill amount is: { sum } \n " "Thanks for shopping with us" ) break

Python Exercise 4 --- "Number Guessing Game"

  Python Exercise 4: "Number Guessing Game" Q: You have to build a  "Number Guessing Game,"  in which a winning number is set to some integer value. The Program should take input from the user, and if the entered number is less than the winning number, a message should display that the number is smaller and vice versa. Instructions: 1. You are free to use anything we've studied till now. 2. The number of guesses should be limited, i.e (5 or 9). 3. Print the number of guesses left. 4. Print the number of guesses he took to win the game. 5. “Game Over” message should display if the number of guesses becomes equal to 0. You are advised to participate in solving this problem. This task helps you become a good problem solver and helps you accept the challenge and acquire new skills. Solution: print ( "Hii! Welcome, \n " , "This a number guessing game, " "made by Saswata \n " , "Let's Start \n " ) n...

Exercise 3 --- "Calculator"

 Exercise 3 ---  "Calculator" Q:  Design a calculator which will correctly solve all the problems: Solution: # Exercise 3 - Calculator """Design a calculator which will correctly solve all the problems:""" # Solution : # Input Part Calculator_On = True print ( 'Welcome, Your Calculator is ready to serve you !' ) while (Calculator_On == True ): x = int ( input ( 'Please enter First number : \n ' )) y = int ( input ( 'Please enter Second number : \n ' )) operator = input ( 'Please Select Operator from these {*,/,+,-} : ' ) # Output Part if (operator == '*' ): print ( 'Output : ' , x * y) elif (operator == '/' ): print ( 'Output : ' , x / y) elif (operator == '+' ): print ( 'Output : ' , x + y) elif (operator == '-' ): print ( 'Output : ' , x - y) print ( ' Do you want to ...

Python Exercise 2 - "Faulty Calculator"

 Python Exercise 2 -  "Faulty Calculator" Q:     Design a calculator which will correctly solve all the problems except the following ones:    45 * 3 = 555, 56+9 = 77, 56/6 = 4   Your program should take operator & the two numbers as input from the user and then return the result. Solution: # Input Part Calculator_On = True print ( 'Welcome, Your Calculator is ready to serve you !' ) while (Calculator_On == True ): x = int ( input ( 'Please enter First number : ' )) y = int ( input ( 'Please enter Second number : ' )) operator = input ( 'Please Select Operator from these {*,/,+,-} : ' ) # Output Part if (operator == '*' ): if (x == 45 and y == 3 ): print ( 'Output : ' , 555 ) else : print ( 'Output : ' , x * y) elif (operator == '/' ): if (x == 56 and y == 6 ): print ( 'Output : ' , 4 ) else : prin...

Python Exercise 1--- Dictionary

 Python Exercise 1--- So, in this tutorial i.e. Python exercise 1 tutorial, what we have to do is, we have to create a dictionary, similar to the real-world dictionary. There is no limit to the definition you provide to any word as this exercise is just for your practice. The details and functionalities that are essential and must be present are: User will give a word as an input. Suppose that the word is present in your dictionary along with its definition or meaning. The program will print the meaning or definition of that word. For example: The user inputs the word : “programming” The output will be:  "the process of writing computer programs" Your main focus should be towards writing a neat and efficient code, using only the knowledge from our previously done tutorials. CODE--- #Exercise 1--- dic = { "A" : "Apple" , "B" : "Ball" , "C" : "Cat" , "D" : "Dog" } print ( "The required meaning ...