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/01/29/01-character-input.html | |
| import datetime | |
| import math | |
| name = input("Hey Bud! What's your name? ") | |
| # by converting to float before forcing to int I avoid errors when user inputs decimal | |
| age = int(float(input("Hi! "+ name +" How old are you? "))) | |
| # round up to make sure number isn't 0 | |
| mult = math.ceil(float(input("Please enter a 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/exercise/2014/02/05/02-odd-or-even.html | |
| # in case floats are input force them to int | |
| num = int(float(input("Please enter a whole number. "))) | |
| check = int(float(input("Please enter another whole number. "))) | |
| # set message depending on if num / check leaves a remainder | |
| if num % check == 0: | |
| message = str(num) + ' can be divided evenly by ' + str(check) + ' and the result is ' + str(int(num/check)) | |
| else: |
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/02/15/03-list-less-than-ten.html | |
| a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] | |
| def less_than_ten(a): | |
| for n in a: | |
| if n < 10: | |
| print(n) | |
| less_than_ten(a) |
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/02/26/04-divisors.html | |
| import math | |
| invar = input("Please enter an integer. Then I'll return a list of all divisors of that number: ") | |
| def divisors(n): | |
| divisors = [ x for x in range(1,n+1) if n % x == 0] | |
| print(*divisors, sep=', ') | |
| try: | |
| # deal with float inputs |
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/05/05-list-overlap.html | |
| import random | |
| a = random.sample(range(1, 100), 10) | |
| b = random.sample(range(1, 100), 10) | |
| c = list(set(a).intersection(b)) | |
| print('a:', a, '\nb:', b, '\noverlap:',c) |
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
| import requests | |
| from bs4 import BeautifulSoup | |
| url = 'https://www.nytimes.com/' | |
| result = requests.get(url) | |
| c = result.content | |
| soup = BeautifulSoup(c) | |
| heading = soup.find_all('h2') | |
| for story in heading: | |
| print(story.text.strip()) |
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
| def stringlists(): | |
| s = input("please enter a string a.k.a. some text ") | |
| # extended slice syntax with negative 1 step reverses the string ([begin:end:step]) | |
| if s[::-1].lower() == s.lower(): | |
| return 'Cool! That string was a palindrome.' | |
| else: | |
| return "That string wasn't a palindrome." |
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
| a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] | |
| even = [n for n in a if n%2 == 0] | |
| odd = [n for n in a if n%2 != 0] | |
| print('even:', even, '\nodd:', odd) |
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
| def rps(): | |
| print('ROCK - PAPER - SCISSORS: \nRock beats scissors \nScissors beats paper \nPaper beats rock\n') | |
| while True: | |
| yn = input("Want to play? Yes or No: ").lower() | |
| if yn[0] == 'y': | |
| play() | |
| else: | |
| print('\nOK. Maybe later.') | |
| break |
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
| import random | |
| num = random.randrange(0,100,1) | |
| score = 0 | |
| while True: | |
| guess = input("Guess the number between 1 and 100? -- Type exit to end. ") | |
| if guess.lower() == 'exit': | |
| break | |
| else: | |
| gnum = int(guess) |