This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Create a program that will play the “cows and bulls” game with the user. The game works like this: | |
| #Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. Keep track of the number of guesses the user makes throughout teh game and tell the user at the end. | |
| #Say the number generated by the computer is 1038. An example interaction could look like this: | |
| # Welcome to the Cows and Bulls Game! | |
| # Enter a number: | |
| # >>> 1234 | |
| # 2 cows, 0 bulls | |
| # >>> 1256 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method. | |
| # Extra: | |
| # Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list. | |
| import random | |
| def password_generator(): | |
| user_input = input("For a strong password, type 'strong'. For a weak one, type 'weak': ") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. For example, say I type the string: | |
| # My name is Michele | |
| # Then I would see the string: | |
| # Michele is name My | |
| # shown back to me. | |
| #https://www.practicepython.org/exercise/2014/05/21/15-reverse-word-order.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates. | |
| # Extras: | |
| # Write two different functions to do this - one using a loop and constructing a list, and another using sets. | |
| # https://www.practicepython.org/exercise/2014/05/15/14-list-remove-duplicates.html | |
| def remove_dupe_set_version(lst): | |
| return list(set(lst)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Write a program that asks the user how many Fibonnaci numbers to generate and then generates them. Take this opportunity to think about how you can use functions. Make sure to ask the user to enter the number of numbers in the sequence to generate.(Hint: The Fibonnaci seqence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, …) | |
| #https://www.practicepython.org/exercise/2014/04/30/13-fibonacci.html | |
| def fibonnaci(): | |
| user_input = int(input('Please enter how many Fibonnaci numbers to generate: ')) | |
| fibonnaci_list = [1, 1] | |
| if user_input <= 0: | |
| print([]) | |
| elif user_input == 1: | |
| print([1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Write a program that takes a list of numbers (for example, a = [5, 10, 15, 20, 25]) and makes a new list of only the first and last elements of the given list. For practice, write this code inside a function. | |
| #https://www.practicepython.org/solution/2014/05/15/12-list-ends-solutions.html | |
| def get_first_and_last_num(list_of_num): | |
| return list_of_num if len(list_of_num) < 3 else [list_of_num[0], list_of_num[-1]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Ask the user for a number and determine whether the number is prime or not. (For those who have forgotten, a prime number is a number that has no divisors.). | |
| #https://www.practicepython.org/exercise/2014/04/16/11-check-primality-functions.html | |
| def is_prime_number(): | |
| user_input = int(input("Please enter a number and let's determine if it is a prime number or not: ")) | |
| prime_list = [divisor for divisor in range(1, user_input + 1) if user_input % divisor == 0] | |
| print('Not a prime number') if len(prime_list) > 2 else print('Prime number') | |
| is_prime_number() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #https://www.practicepython.org/solution/2014/04/10/09-guessing-game-one-solutions.html | |
| #Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (_Hint: remember to use the user input lessons from the very first exercise | |
| #Extras: | |
| #Keep the game going until the user types “exit” | |
| #Keep track of how many guesses the user has taken, and when the game ends, print this out. | |
| import random |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://www.practicepython.org/solution/2014/04/02/08-rock-paper-scissors-solutions.html | |
| #Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game) | |
| #I used getpass module so players cannot see each other's answers | |
| import getpass | |
| def rock_paper_scissors(): | |
| print('Welcome to Rock, Paper, Scissors!') | |
| start_game = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://www.practicepython.org/exercise/2014/03/19/07-list-comprehensions.html | |
| # Let’s say I give you a list saved in a variable: a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write one line of Python that takes this list a and makes a new list that has only the even elements of this list in it. | |
| new_list = [num for num in a if num % 2 == 0] |
NewerOlder