You are on page 1of 6

In 

[19]: from math import sqrt

def triangle(a, b):


c = sqrt(a**2 + b**2)
return c

def main():
print("Input lengths of shorter triangle sides:")
side_a= float(input("a: "))
side_b = float(input("b: "))

print("The result is: %.2f"%triangle(side_a, side_b))

main()

Input lengths of shorter triangle sides:


a: 5
b: 6
The result is: 7.81

In [18]: def shipping(item_number):


first_item = 10.95
subseq_item = 2.95

total = first_item + (subseq_item * (item_number - 1))

return total

def main():
amount = int(input("Number of items: "))
print("The shiping charge: ", shipping(amount))

main()

Number of items: 365


The shiping charge: 1084.75

In [36]: def median(a, b, c):


if a > b:
if a < c:
return a

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
elif b > c:
return b
else:
return c
else:
if a > c:
return a
elif b < c:
return b
else:
return c

def main():
num_a = int(input("First number: "))
num_b = int(input("Second number: "))
num_c = int(input("Third number: "))

print("The median is: ", median(num_a, num_b, num_c))

main()

First number: 5
Second number: 6
Third number: 8
The median is: 6

In [42]: def is_triangle(a, b, c):


if (a < b + c) and (b < a + c) and (c < a + b):
return True
return False

def main():
side_a = int(input())
side_b = int(input())
side_c = int(input())

if is_triangle(side_a, side_b, side_c):


print("Triangle is valid")
else:
print("Triangle is not valid")

main()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
6
2
2
Triangle is not valid

In [6]: def isPrime(n):


if n == 1:
return False
if n == 2:
return True
else:
for x in range(2, n):
if n % x == 0:
return False
return True

def main():
number = int(input("Enter a number: "))
if isPrime(number):
print("{} is a prime number".format(number))
else:
print("{} is not a prime number".format(number))

if __name__ == "__main__":
main()

Enter a number: 2
2 is a prime number

In [31]: from random import randint

def randomPassword():
length = randint(7, 10)
password = ""
for i in range(length):
char = chr(randint(33, 126))
password = password + char
return password

def main():
print("The random password is: ", randomPassword())

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if __name__ == "__main__":
main()

The random password is: >>oX^_2f]

In [34]: def goodPassword(p):


x = False
y = False
z = False

for i in p:
if i >= "A" and i <= "Z":
x = True
if i >= "a" and i <= "z":
y = True
if i >= "0" and i <= "9":
z = True
if len(p) >= 8 and x and y and z:
return True
return False

def main():
password = input("Enter a passowrd: ")
if goodPassword(password):
print("Good password")
else:
print("Entered password is not valid!")

if __name__ == "__main__":
main()

Enter a passowrd: jnlkmKGBVFD5665321


Good password

In [33]: from random import randint

def randomPassword():
length = randint(7, 10)
password = ""
for i in range(length):
char = chr(randint(33, 126))
password = password + char
return password

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
def goodPassword(p):
x = False
y = False
z = False

for i in p:
if i >= "A" and i <= "Z":
x = True
if i >= "a" and i <= "z":
y = True
if i >= "0" and i <= "9":
z = True
if len(p) >= 8 and x and y and z:
return True
return False

def main():
attempt = 0
good_password = ""

while not goodPassword(good_password):


good_password = randomPassword()
attempt = attempt + 1

print(attempt)
print(good_password)

if __name__ == "__main__":
main()

2
T;@<l7."g-

In [57]: import random

def licensePlate():
list_alph = "A B C D E F J H I J K L M N O P Q R S T U V W X Y Z".split()
list_int = '1 2 3 4 5 6 7 8 9 0'.split()
length = random.randint(3,4)

license = ""

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
if length == 3:
for i in range(6):
if i <= 2:
n = random.randint(0, 25)
license = license + list_alph[n]
else:
n = random.randint(0, 9)
license = license + list_int[n]
elif length == 4:
for i in range(7):
if i <= 3:
n = random.randint(0, 9)
license = license + list_int[n]
else:
n = random.randint(0, 25)
license = license + list_alph[n]
print(license)

def main():
licensePlate()

main()

QEE367

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like