Last active
July 10, 2016 00:01
-
-
Save 5t111111/4e3f8a838d9d1de8844e530314d580c1 to your computer and use it in GitHub Desktop.
Doing Math with Python Chapter 1 programming challenge
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 odd_or_even(x): | |
if x % 2 == 0: | |
return 'even' | |
else: | |
return 'odd' | |
def next_nine_number(x): | |
for i in range(0, 9): | |
x = x + 2 | |
yield x | |
if __name__ == '__main__': | |
n = input('Enter a number: ') | |
n = float(n) | |
if n.is_integer(): | |
n = int(n) | |
print(odd_or_even(n)) | |
for i in next_nine_number(n): | |
print(i) | |
else: | |
print('Please enter an integer') |
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 multi_table_2(a, times): | |
for i in range(1, times+1): | |
print('{0} x {1} = {2}'.format(a, i, a*i)) | |
if __name__ == '__main__': | |
a = input('Enter a number: ') | |
b = input('How many answers do you want?: ') | |
multi_table_2(float(a), int(b)) |
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 print_menu(): | |
print('1. Kilometers to Miles') | |
print('2. Miles to Kilometers') | |
print('3. Kilograms to Pounds') | |
print('4. Pounds to Kilograms') | |
print('5. Celsius to Fahrenheit') | |
print('6. Fahrenheit to Celsius') | |
def km_miles(): | |
km = float(input('Enter distance in kilometers: ')) | |
miles = km / 1.609 | |
print('Distance in miles: {0}'.format(miles)) | |
def miles_km(): | |
miles = float(input('Enter distance in miles: ')) | |
km = miles * 1.609 | |
print('Distance in kilometers: {0}'.format(km)) | |
def kg_pounds(): | |
kg = float(input('Enter weight in kilograms: ')) | |
pounds = kg * 2.20462262 | |
print('Weight in pounds: {0}'.format(pounds)) | |
def pounds_kg(): | |
pounds = float(input('Enter weight in pounds: ')) | |
kg = pounds / 2.20462262 | |
print('Weight in kilograms: {0}'.format(kg)) | |
def celsius_f(): | |
celsius = float(input('Enter temperature in celsius: ')) | |
fahrenheit = celsius * (9 / 5) + 32 | |
print('Temperature in fahrenheit: {0}'.format(fahrenheit)) | |
def fahrenheit_c(): | |
fahrenheit = float(input('Enter temperature in fahrenheit: ')) | |
celsius = (fahrenheit - 32) * (5 / 9) | |
print('Temperature in celsius: {0}'.format(celsius)) | |
if __name__ == '__main__': | |
print_menu() | |
choice = input('Which conversion would you like to do? ') | |
if choice == '1': | |
km_miles() | |
if choice == '2': | |
miles_km() | |
if choice == '3': | |
kg_pounds() | |
if choice == '4': | |
pounds_kg() | |
if choice == '5': | |
celsius_f() | |
if choice == '6': | |
fahrenheit_c() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment